I am not sure what you mean by routing to an implementation "in a generic
way".

You can clean up your code a lot by merging your "Model" type with "Impl".
This is the more idiomatic way of writing in Elm:


type alias Model =
      CreateInvoiceImpl CreateInvoice.Model
      | SendTextImpl SendText.Model

update : Msg -> Model -> Model
update message model =
  case message of
    CreateInvoiceMsg msg ->
      CreateInvoice.update msg data
    SendTextMsg msg ->
      SendText.update msg data

On Tue, Aug 23, 2016 at 9:58 AM, Larry Weya <larryw...@gmail.com> wrote:

> Thank you for the response, routing to the proper implementation in a
> generic way is my problem. The command implementation should receive the
> json value and take care of decoding it.
>
> I ended up implementing it using case expressions which is ok but means I
> need to update the module in a number of place anytime I add a new command.
> I've pushed the code here
> <https://github.com/larryweya/dynamic-elm-modules/blob/master/CommandImpl.elm>
> if anyone wants to have a look.
>
> The issue is with the module that manages the commands (CommandImpl.elm)
> and how to defined a "generic" type which would hold a different kind of
> message and model depending on the command's name
>
> On Tuesday, 23 August 2016 18:04:13 UTC+3, Nick H wrote:
>>
>> This sounds like exactly the use case for Json.Decode.andThen
>> <http://package.elm-lang.org/packages/elm-lang/core/4.0.5/Json-Decode#andThen>.
>> (See that documentation for an example)
>>
>> Your could have your main module decode the JSON "params" enough to
>> figure out which type of message you've received, then route it to the
>> proper implementation.
>>
>> On Mon, Aug 22, 2016 at 4:25 AM, Larry Weya <larr...@gmail.com> wrote:
>>
>>> I have a project where I would like to have different implementations
>>> depending on a provided value.
>>>
>>> I have what we are referring to as Commands and we are using elm provide
>>> a UI used to configure a command's parameters so e.g. we have 2 Commands, a
>>> CreateInvoice command and a SendText command. They are persisted in a
>>> database as JSON. To edit the command, I load up the JSON and depending on
>>> the command's name I'd like my Main module to use the commands
>>> init/view/update functions. The biggest issue is that CreateInvoice and
>>> SendText models have different types.
>>>
>>> Main.elm
>>>
>>> -- how do I define a model that can accomodate both types
>>>
>>> main =
>>>  Html.programWithFlags{...}
>>>
>>> init : String -> Json.Encode.Value -> Model
>>> init (moduleName, params) =
>>>  -- how to init a command based on the moduleName
>>>
>>>
>>> CreateInvoice.elm
>>>
>>> type alias Model = {amount : Int}
>>>
>>> init jsonValue =
>>>  -- decode json value into model
>>>
>>> SendText.elm
>>>
>>> type alias Model = {message : String}
>>>
>>> init jsonValue =
>>>  -- decode json value into model
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Elm Discuss" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to elm-discuss...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Elm Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to elm-discuss+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to