zrho / purescript-optic-ui

PureScript UI framework based on lenses.
122 stars 10 forks source link

Async calls in initializer in the latest version #21

Open clayrat opened 8 years ago

clayrat commented 8 years ago

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?

clayrat commented 8 years ago

I mean, the button works, but the initializer does not.

zrho commented 8 years ago

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.