【问题标题】:trigger an Action from the Update function从更新函数触发一个动作
【发布时间】:2016-01-01 20:39:07
【问题描述】:

遇到了一个简单的问题。当我在更新函数中收到操作 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])

【问题讨论】:

    标签: elm


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      相关资源
      最近更新 更多