From dc9e34207a2eea6992edf3ca9c30875ab47169c4 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Tue, 1 Sep 2020 23:49:23 -0700 Subject: [PATCH] Init local model with load from global model. --- examples/docs/src/Global.elm | 10 +++--- examples/docs/src/Template/Documentation.elm | 2 +- .../src/generate-template-module-connector.js | 34 +++++++++++++++++-- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/examples/docs/src/Global.elm b/examples/docs/src/Global.elm index 68281355..a08d4be6 100644 --- a/examples/docs/src/Global.elm +++ b/examples/docs/src/Global.elm @@ -62,11 +62,13 @@ type Msg | Increment -init : a -> Model +init : a -> ( Model, Cmd Msg ) init maybePagePath = - { showMobileMenu = False - , counter = 0 - } + ( { showMobileMenu = False + , counter = 0 + } + , Cmd.none + ) update : Msg -> Model -> ( Model, Cmd Msg ) diff --git a/examples/docs/src/Template/Documentation.elm b/examples/docs/src/Template/Documentation.elm index 6986feae..db402561 100644 --- a/examples/docs/src/Template/Documentation.elm +++ b/examples/docs/src/Template/Documentation.elm @@ -54,7 +54,7 @@ load globalModel model = save : Model -> Global.Model -> Global.Model save model globalModel = - globalModel + { globalModel | counter = model.counter } init : Documentation -> ( Model, Cmd Msg ) diff --git a/generator/src/generate-template-module-connector.js b/generator/src/generate-template-module-connector.js index 385d540b..ac961a83 100644 --- a/generator/src/generate-template-module-connector.js +++ b/generator/src/generate-template-module-connector.js @@ -114,8 +114,11 @@ init : } -> ( Model, Cmd Msg ) init currentGlobalModel maybePagePath = - ( { global = currentGlobalModel |> Maybe.withDefault (Global.init maybePagePath) - , page = + let + ( globalModel, globalCmd ) = + currentGlobalModel |> Maybe.map (\\m -> ( m, Cmd.none )) |> Maybe.withDefault (Global.init maybePagePath) + + page = case maybePagePath |> Maybe.map .metadata of Nothing -> NotFound @@ -128,12 +131,22 @@ init currentGlobalModel maybePagePath = |> Model${name} `).join("\n ")} + + ( templateModel, templateCmd ) = + load globalModel page + in + ( { global = globalModel + , page = templateModel , current = maybePagePath } - , Cmd.none + , Cmd.batch + [ templateCmd + , globalCmd |> Cmd.map MsgGlobal + ] ) + update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case msg of @@ -176,6 +189,21 @@ update msg model = +load : Global.Model -> TemplateModel -> ( TemplateModel, Cmd Msg ) +load globalModel model = + case model of + ${templates.map(name => `Model${name} m -> + Template.${name}.template.load globalModel m + |> Tuple.mapFirst (Model${name}) + |> Tuple.mapSecond (Cmd.map Msg${name}) +` + ).join("\n ")} + + NotFound -> + ( model, Cmd.none ) + + + save : TemplateModel -> Global.Model -> Global.Model save model globalModel= case model of