I am trying to add subscriptions as I have a dropdown, this helps ensure that the dropdowns automatically close when you click outside of them. On doing so, I had to change the model as well as my update.
This link (will take you to the Elm Bootstrap site) is the dropdown I am working with which is using Bootstrap 4.
Error I am getting
The 1st argument to
sandboxis not what I expect:295| Browser.sandbox 296|> { init = initialModel 297|>
, update = update 298|> , view = view 299|> }This argument is a record of type:
{ init : ( Model, Cmd Msg ) , update : Msg -> Model -> ( Model, Cmd Msg ) , view : Model -> Html Msg }But
sandboxneeds the 1st argument to be:{ init : ( Model, Cmd Msg ) , update : Msg -> ( Model, Cmd Msg ) -> ( Model, Cmd Msg ) , view : ( Model, Cmd Msg ) -> Html Msg }
Alias Model
type alias Model =
{ currentNumber : Int, clicks : Int, outputList : List(String), uniqueValues : Dict Int Int, firstNumber : String, secondNumber : String, myDropState : Dropdown.State, items : List String, selectedItem : String, dictKeyToRemove : String,
modalVisibility : Modal.Visibility }
Initial Model
initialModel : (Model, Cmd Msg)
initialModel =
({ currentNumber = 0, clicks = 0, outputList = [""], uniqueValues = Dict.empty, firstNumber = "", secondNumber = "", myDropState = Dropdown.initialState, items = ["Small", "Medium", "Large"], selectedItem = "Small", dictKeyToRemove = "",
modalVisibility = Modal.hidden }, Cmd.none)
Main
main : Program () Model Msg
main =
Browser.sandbox
{ init = initialModel
, update = update
, view = view
}
Subscriptions
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Dropdown.subscriptions model.myDropState DropMsg ]
Update
update : Msg -> Model -> ( Model, Cmd Msg)
update msg model =
case msg of
DropMsg state ->
({model | myDropState = state }, Cmd.none)
I am not sure what I am missing at this point, I have tried changing the argument with no luck.
Browser.sandboxwill create a simple and very limited program. The dropdown requires capabilities beyond that, namely subscriptions, which means you need to use eitherBrowser.elementorBrowser.documentinstead.The type of
Browser.elementis:Compared to
Browser.sandbox:There are three differences here:
inittakes an argument,flags, which can be anything and will be interpreted by the runtime according to its type. For your purpose just using()should be enough (which is essentially whatsandboxdoes), but see the flags section of the guide for more details.initandupdatereturns( model, Cmd msg )instead of justmodel. This is the root cause of your error, because you haveupdateandinitfunctions which return( model, Cmd msg )aselementwould expect, but try to feed them tosandbox. This makes the compiler unhappy, because it thinks thatmodelshould be( Model, Cmd msg )instead of justModel.elementexpects an additionalsubscriptionsfunction, which you have defined but currently aren't doing anything with since sandbox doesn't accept it.Putting this all together, substituting the following
mainfunction should work for you: