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 <larryw...@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+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