vendredi 11 août 2017

Change widget model from parent in Elm

I am using the "compose" pattern in Elm.

In Main.elm, I am generating an initial seed for Random using the following:

type alias Model =
    { ...
    , seed : Seed
    }

initialModel : Model
initialModel =
    { ...
    , seed = initialSeed 0
    }

init : ( Model, Cmd Msg )
init =
    ( initialModel, generate InitializeSeed (int minInt maxInt) )

type Msg
    = ...
    | InitializeSeed Int

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        ...

        InitializeSeed value ->
            ( { model | seed = Random.initialSeed value }, Cmd.none )

which seems to work well for initializing the random number generator seed to a random starting value.

I have an "independent" widget that is used in the main application. I would like to pass the seed down to that widget on receipt of the InitializeSeed message and then retrieve the value as it changes in the child widget (using Random.step) so that I can then update other widgets' seeds as they need the generator.

How can I generate a message for the child widget from the update function in Main so that I can pass the seed down to the child? How can the child return the updated seed back to the parent?




Aucun commentaire:

Enregistrer un commentaire