从Update函数触发Action

时间:2016-01-01 20:39:07

标签: elm

有一个希望简单的问题。当我在更新函数中收到操作A时,我想返回执行某些操作的任务,然后生成操作B,再次由更新函数接收。我的理解是,从Update返回的任何效果都将由startapp执行,但似乎没有发生任何事情。这是一个缩小的例子:

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 个答案:

答案 0 :(得分:2)

除了StartApp之外,

main还需要一个端口来执行任务。更改您的main功能并添加tasks这样的端口,您将全部设置完毕:

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

main =
  app.html

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