从更新功能触发动作 [英] trigger an Action from the Update function
问题描述
解决了一个可能很简单的问题。当我在更新功能中收到动作A时,我想返回一个执行某些任务的任务,然后执行动作B,该动作又被更新功能接收到。我的理解是,无论从Update返回的任何效果都将由startapp执行,但似乎什么也没有发生。下面是一个缩略的示例:
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])
推荐答案
StartApp
除 main
外还需要一个用于执行任务的端口。更改您的 main
函数并添加 tasks
端口,您将被设置如下:
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
这篇关于从更新功能触发动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!