Getting an XHR Going

42 views
Skip to first unread message

Lally Singh

unread,
Mar 8, 2016, 9:50:49 AM3/8/16
to reflex-frp
I'm trying to have a <div> show the results of an XHR (that returns JSON from a yesod app).

But, I can't seem to figure out how to effectively create an Event t XhrRequest.  I can create an XhrRequest, but that's about it.  I tried updated $ constDyn $ xhrRequest "GET" url def, which compiled, but didn't actually do the request.  I feel that I'm missing something quite fundamental here.  Is there another event, like onLoad, that I should piggyback into for creating the XhrRequest?

Thanks in advance!

Sean Leather

unread,
Mar 8, 2016, 10:06:28 AM3/8/16
to refle...@googlegroups.com
Hi Lally,

On Tue, Mar 8, 2016 at 4:50 PM, Lally Singh wrote:
I'm trying to have a <div> show the results of an XHR (that returns JSON from a yesod app).

But, I can't seem to figure out how to effectively create an Event t XhrRequest.  I can create an XhrRequest, but that's about it.  I tried updated $ constDyn $ xhrRequest "GET" url def, which compiled, but didn't actually do the request.  I feel that I'm missing something quite fundamental here.  Is there another event, like onLoad, that I should piggyback into for creating the XhrRequest?

Does newEventWithTriggerRef help you? You can then trigger an event with an IO function.

Personally, I prefer this simplifying wrapper:

newEventAndTrigger :: MonadWidget t m => m (Event t a, a -> IO ())
newEventAndTrigger = do
  postGui <- askPostGui
  runWithActions <- askRunWithActions
  (e, triggerRef) <- newEventWithTriggerRef
  return (e, \a ->
    readRef triggerRef >>=
    maybe (return ()) (\t -> postGui $ runWithActions [t :=> a]))

Regards,
Sean

Lally Singh

unread,
Mar 9, 2016, 4:03:04 PM3/9/16
to reflex-frp
Sean, thank you.
I started at newEventAndTrigger and kept digging a bit.  I found getPostBuild, which seemed to do the trick.  Is that reasonable, or is it only working because of bad luck? 

Sean Leather

unread,
Mar 10, 2016, 1:15:58 AM3/10/16
to refle...@googlegroups.com
Perfectly reasonable! Now I understand what you meant by onLoad. I was thinking of the XHR onload(), but you meant the point at which the DOM was loaded/ready. That's exactly what getPostBuild is intended for.

Regards,
Sean
Reply all
Reply to author
Forward
0 new messages