How to get arbitrary values from an Event

I’m trying to write an FFI module for Choices.js

The event that gets passed can carry data with it, see (also take a look at the example above).

When i look here i don’t see a function that takes a key name (or a property path for complex situations).

I want to access these values, then rewrap them into a purescript type and then offer this as an export of the FFI module i’m making.

How can i access all values from the event ??

Yeah, the web-events library only provides for things that are mentioned in the spec specifically.

Personally, I’d probably implement accessors for custom things from events in an FFI module in my app. Alternatively you could coerce to Foreign, and use the functions for that to read from it.

@garyb do you mean accessor functions in the JS side of the app? I could do that … just that constructing the purescript value in JS is not so easy. So i could end up with 7 accessor functions in JS and one construction function on the purescript side.

Thanks for your advice, i will look at coercering to Foreign first to keep it all contained in a single function. Why is this not your preference ?

I wrote this (type checked but didn’t run yet)

type AddItemEvent =
  { id :: Int
  , value :: String
  , label :: String
  , customProperties :: Foreign
  , groupValue :: String
  , keyCode :: Int

unsafeConvertEvent :: forall a b c. (Foreign -> ExceptT c Identity b) -> a -> b
unsafeConvertEvent f e = unsafePartial $ fromRight $ runExcept $ f $ unsafeToForeign e

onAddItem :: forall r i. (AddItemEvent -> Maybe i) -> HP.IProp r i
onAddItem f = HE.handler (EventType "addItem") (\e -> f (unsafeConvertEvent convert e))
  where convert e = do
          id               <- e ! "id" >>= readInt 
          value            <- e ! "value" >>= readString
          label            <- e ! "label" >>= readString
          customProperties <- e ! "customProperties"
          groupValue       <- e ! "groupValue" >>= readString
          keyCode          <- e ! "keyCode" >>= readInt
          pure { id, value, label, customProperties, groupValue, keyCode }