从 Update 函数触发一个 Action
trigger an Action from the Update function
有一个希望很简单的问题。当我在我的更新函数中收到操作 A 时,我想 return 一个执行某些操作的任务,然后导致操作 B,它再次被更新函数接收。据我了解,从 Update return 编辑的任何效果都将由 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])
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
有一个希望很简单的问题。当我在我的更新函数中收到操作 A 时,我想 return 一个执行某些操作的任务,然后导致操作 B,它再次被更新函数接收。据我了解,从 Update return 编辑的任何效果都将由 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])
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