purescript-optic-ui
purescript-optic-ui copied to clipboard
Async calls in initializer in the latest version
Is it possible now to do an async call to initialize a component? I have something like this, where I want an image loaded on startup and a button to reload it:
imageLoader = withAsync $ with \st h -> let
setUrl u = updatePure h $ case u of
Just a -> st # imgUrl .~ Success a
Nothing -> st # imgUrl .~ Failed unit
reload = update h $ do
async (JS.decode <<< _.response <$> (AJ.get someUrl)) (const Nothing)
pure st
in withView (H.div $ [ H.onInitialized "foo" $ const reload ]) $ mconcat
[ imgUrl $ mconcat
[ _Failed $ ui $ H.p_ $ text "error"
, _Success $ with \s _ -> ui $ H.div_ $ H.img [ H.srcA $ extractUrl s ] mempty
]
, ui $ H.button [ H.onClick $ const reload ] $ text "reload"
, listen (const true) setUrl
]
But it doesn't seem to fire. Maybe it's possible to hook it up via the _Init prism somehow?
I mean, the button works, but the initializer does not.
Oh, in theory this should work; probably there is something wrong with how initializers are attached to the vdom. I'll have a look at it as soon as possible.