programWithFlags for App using elm-mdl

399 views
Skip to first unread message

Robert Walter

unread,
Jul 29, 2016, 7:14:39 AM7/29/16
to Elm Discuss
Hello,

I try to initialize my elm application using programWithFlags.

My app makes use of elm-mdl, so my model looks something like this:

type alias AppState =
   
{ mdl : Material.Model
   
, viewSelected : Int
   
, ...
   
, titleEditable : Bool
   
, refreshEditorContent : Bool
   
}

In other words, it contains a Material.Model that I need to make proper use of elm-mdl.

When I try to use App.programWithFlags, however, having Material.Model in my AppState leads to the following error:


-- BAD FLAGS ------------------------------------------------------- src/App.elm
Your `main` is demanding an unsupported type as a flag.
68| main =
    ^^^^
The specific unsupported type is:
    Dict.Dict
        (List Int)
        { animation : Material.Ripple.Animation
        , ignoringMouseDown : Bool
        , metrics :
              Maybe
                  { rect :
                        { height : Float
                        , left : Float
                        , top : Float
                        , width : Float
                        }
                  , x : Float
                  , y : Float
                  }
        }
The types of values that can flow through in and out of Elm include:
    Ints, Floats, Bools, Strings, Maybes, Lists, Arrays, Tuples, Json.Values,
    and concrete records.

Currently, I use localStorage to store my AppState partially (I do not encode 'mdl'). I don't see how I can make this work, however, since I assume that programWithFlags can only decode the types mentioned in the BAD FLAGS message and it expects to receive a completely serialized AppState.

Any suggestions from the coomunity?

Thanks,
Robert


Sergey Skrynnikov

unread,
Jul 29, 2016, 7:25:48 AM7/29/16
to Elm Discuss
Hi Robert,

your flags don't have to be of the same type as the model - try modifying your 'init' function to take only your partial state and return (Model, Cmd Msg).

пятница, 29 июля 2016 г., 14:14:39 UTC+3 пользователь Robert Walter написал:

Robert Walter

unread,
Jul 29, 2016, 7:29:58 AM7/29/16
to Elm Discuss
Oh man, I just found that out as well. Thank you.

So, I changed it like so:

type alias AppState =
   
{ mdl : Material.Model
   
, viewSelected : Int
   
, ...
   
, titleEditable : Bool
   
, refreshEditorContent : Bool
   
}


type
alias Flags =
   
{ viewSelected : Int

   
, ...
   
, titleEditable : Bool
   
, refreshEditorContent : Bool
    }

init : Flags -> ( AppState, Cmd Msg )
init flags =
    ( defaultOrFromFlags flags, Layout.sub0 Mdl )

Thanks again!

Robert Walter

unread,
Jul 29, 2016, 7:55:10 AM7/29/16
to Elm Discuss
A better way to this is to use Maybe_s:

main : Program (Maybe Flags)
main
=
   
App.programWithFlags
       
{ init = init
       
, view = view
       
, update = update
       
, subscriptions = subscriptions
       
}




init
: Maybe Flags -> ( AppState, Cmd Msg )
init maybeFlags
=
    let
        appStateInit
=
           
case maybeFlags of
               
Nothing ->
                    defaultAppState


               
Maybe.Just flags ->
                    appStateFromFlags flags
   
in
       
( appStateInit, Layout.sub0 Mdl )
Reply all
Reply to author
Forward
0 new messages