trigger an Action from the Update function

拟墨画扇 提交于 2019-12-31 05:41:28

问题


Got a hopefully simple problem. When I receive action A in my update function, I'd like to return a task that does some stuff, then results in action B, which is received by the update function again. Its my understanding that whatever effects are returned from Update will be executed by startapp, but nothing seems to be happening. Here's a whittled down example:

import StartApp exposing (start)
import Effects
import Task
import Html exposing (..)
import Html.Events exposing (..)

main =
  (start
    { init = init
    , update = update
    , view = view
    , inputs = []
    }).html


type Action
    = Click
    | Dummy

type alias Model =
    { clicks: Int
    }


init : (Model, Effects.Effects Action)
init = (Model 0, Effects.none)

update : Action -> Model -> (Model, Effects.Effects Action)
update action model =
  case action of
    Click ->
      -- the click is received.
      (model, Effects.task (Task.succeed Dummy))
    Dummy ->
      -- this never happens!!
      ({ model | clicks = model.clicks + 1}, Effects.none)


view : Signal.Address Action -> Model -> Html
view address model =
  let start = button [ onClick address Click ] [ text (toString model.clicks) ]
  in
      div [] ([start])

回答1:


StartApp requires a port for tasks in addition to main. Change your main function and add the tasks port like this and you'll be all set:

app =
  start
    { init = init
    , update = update
    , view = view
    , inputs = []
    }

main =
  app.html

port tasks : Signal (Task.Task Effects.Never ())
port tasks =
  app.tasks


来源:https://stackoverflow.com/questions/34559838/trigger-an-action-from-the-update-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!