Bzzt
Bzzt

Reputation: 1054

trigger an Action from the Update function

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])

Upvotes: 0

Views: 465

Answers (1)

Chad Gilbert
Chad Gilbert

Reputation: 36375

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

Upvotes: 2

Related Questions