سؤال

I'm using Yesod to build an Ajax app (using jQuery, though I don't think that matters too much for my question). Basically, what I'd like is for the server to send different representations of the same data depending on whether or not the XMLHttpRequest header got sent. (The point of all of this is to use a javascript library like history.js

In particular, I'd like to have a route like:

/picture/#PictureId GET

Which, when accessed without the XHR header, gets handled by going to the default layout -- or better yet, by a widget which will ultimately get wrapped by the default layout, and when accessed by an XHR request, just sends an HTML representation of the widget.

How should I approach this? I guess I can make a custom defaultLayout-like function to wrap Widgets in logic. Is that sensible, or is there a better approach?

Edit: I decided to override the defaultLayout method in the Yesod class to:

defaultLayout widget = do
    req <- waiRequest
    let reqwith = lookup "X-Requested-With" $ requestHeaders req
    when (maybe False (== "XMLHttpRequest") reqwith) $ do
      (PageContent _ _ w) <- widgetToPageContent widget

      giveUrlRenderer $ [hamlet| ^{w} |]
    ...

But now I'm getting a type error I don't quite understand

Couldn't match type `blaze-markup-0.5.1.5:Text.Blaze.Internal.MarkupM ()'
              with `()'
Expected type: HandlerT App IO ()
  Actual type: HandlerT
                 App IO (blaze-markup-0.5.1.5:Text.Blaze.Internal.MarkupM ())
هل كانت مفيدة؟

المحلول

You should take a look at this chapter: http://www.yesodweb.com/book/restful-content.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top