Decoding event.detail.text from custom element

Hello Elm community!!! I need your help understanding how to decode object property from js custom event.

I am using my custom html element in elm like this:

“, node “elm-bridge” [attribute “text” “blabla”, on “todoCompleted” sendTodoCompleted ] []”

my function sendTodoCompleted looks like this:

sendTodoCompleted =
(succeed (TodoCompleted “bla”))

and it works … but instead I would like to read actual property in the js event object ,but I cannot figure out how to do this. So far this is what I tried but it does not work:

sendTodoCompleted msg =
(at [“detail”, “text”] string) msg |> TodoCompleted

actual object shape in event.detail = {text: “bla”}

I do not understand how do I pass/store the decoded value after decoding it?

Your help will be greatly appreciated! Decoders is the most confusing part for me so far.

Niko

You’ll have to map the decoded field to your message:

Json.Decode.map TodoCompleted
    (Json.Decode.at [ "detail", "text" ] Json.Decode.string)

Thank you Steffen this has worked!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.