0.18 possible bug with Html.Events.on?

74 views
Skip to first unread message

Rupert Smith

unread,
Dec 1, 2016, 6:20:12 AM12/1/16
to Elm Discuss
I am using Html.Events.on to pick up changes to a property of a Polymer component:

listbox
        [ items <| Dict.map (\id -> \(Model.Role role) -> Utils.valOrEmpty role.name) model.roleLookup
        , initiallySelected <| Dict.map (\id -> \(Model.Role role) -> Utils.valOrEmpty role.name) model.selectedRoles
        , onSelectedChanged SelectChanged
        ]

With:

onSelectedChanged : (Dict String String -> msg) -> Attribute msg
onSelectedChanged tagger =
    onItemArrayChanged "selected-changed" tagger

onItemArrayChanged : String -> (Dict String String -> msg) -> Attribute msg
onItemArrayChanged propname tagger =
    on propname <| Decode.map tagger <| Decode.map Dict.fromList decodeItems

In other words I am using

on "selected-changed" to trigger notification that the listbox selection has changed.

I've added enough debug statements to figure out that everything is working as it did on 0.17, but that this 'on' Attribute is failing to receive any events in 0.18.

Any thoughts on how to debug further? Anyone else noticed 'on' not working as expected?


Rupert Smith

unread,
Dec 1, 2016, 7:32:39 AM12/1/16
to Elm Discuss
I was able to attach the debugger at the right point to see what is going on in eventHandler.

Seems that for some reason where I was previously decoding a List encoded Dict as json as the value associated with the event, a String is now expected.

 value = Object {ctor: "Err", _0: "Expecting a String at _.detail.value[0] but instead got: ["1","auth-root"]"}

But at least that gives me some clues as to where to look to fix it.

Rupert Smith

unread,
Dec 1, 2016, 10:57:30 AM12/1/16
to Elm Discuss
On Thursday, December 1, 2016 at 12:32:39 PM UTC, Rupert Smith wrote:
Seems that for some reason where I was previously decoding a List encoded Dict as json as the value associated with the event, a String is now expected.

 value = Object {ctor: "Err", _0: "Expecting a String at _.detail.value[0] but instead got: ["1","auth-root"]"}

Turns out it was my json encoders and decoders at fault, as they did not encode and decode the same json.

decodeItems : Decode.Decoder (List ( String, String ))
decodeItems =
    Decode.at [ "detail", "value" ] <|
        Decode.list <|
            Decode.map2 (,) Decode.string Decode.string
 
encodeItems : List ( String, String ) -> Encode.Value
encodeItems items =
    Encode.list
        (List.map (\( idx, val ) -> Encode.list [ Encode.string idx, Encode.string val ]) items)

I changed the decoder to:

decodeItems : Decode.Decoder (List ( String, String ))
decodeItems =
    Decode.at [ "detail", "value" ] <|
        Decode.list <|
            Decode.map2 (,) (Decode.index 0 Decode.string) (Decode.index 1 Decode.string)

and it works. I seem to have opted for a slightly non-standard encoding of a dictionary as an array of arrays, rather than as a json object with name/value fields, but I can fix that.

Strange that it worked in 0.17, I guess the json parsing rules got tightened up a bit.
 
Reply all
Reply to author
Forward
0 new messages