Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalized ajax, allows json etc #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

TobyGoodwin
Copy link

Similar in spirit to the PR of @mwotton, this generalizes ajax with a ToAjax class. Instances allow the request body to be a list of pairs (as in the original code), or a String, a Text, or an aeson Object. A suitable default content type is chosen depending on the type of the data; it can be overridden in AjaxSettings. The response can be a string or an object, and is returned as an aeson Value. Obviously this does introduce a dependency on aeson, but since ghcjs-base depends on aeson I don't see a problem with this.

I've made a yesod-based test framework for ghcjs-jquery ajax calls: https://github.com/TobyGoodwin/ghcjs-jquery-test (It's fairly minimal so far; I'm happy to accept PRs, and of course track any changes that go into ghcjs-jquery.)

@TobyGoodwin
Copy link
Author

In the first version of this PR, the only HTTP method allowed was POST. This was obviously just an oversight. :-)

That's now fixed in f4d9651. This also adds an instance for () when you have no data to send in the ajax request. There's a slightly ugly defaulting method (better ideas anyone?), so that GET is chosen by default when the input is (), and POST is the default for other data. These can of course be overridden in the usual way.

I now have an application (the old "todo" standby) working with ajax calls to a REST API. (The API was originally developed for an Ember implementation, so it's at least vaguely realistic.) Sample ajax calls from this application look reasonable to me:

retrieve = do
  r <- ajax ajaxUrl () def
  ...

update t = do
  r <- ajax (ajaxUrlTodo t) (toJSON t) def { asMethod = PUT }
  ...

@mwotton
Copy link

mwotton commented Mar 16, 2015

looks pretty good to me, but I don't like the defaulting. Is it that
onerous to just pass GET/POST/PUT? it's very little extra code in the
normal case, and has the benefit of simplicity - if someone really really
wants to not have to specify, it'd be simpler for them to write an ajaxGet
function anyway.

mark

On Sun, Mar 15, 2015 at 4:51 AM Toby Goodwin [email protected]
wrote:

In the first version of this PR, the only HTTP method allowed was POST.
This was obviously just an oversight. :-)

That's now fixed in f4d9651
f4d9651.
This also adds an instance for () when you have no data to send in the
ajax request. There's a slightly ugly defaulting method (better ideas
anyone?), so that GET is chosen by default when the input is (), and POST
is the default for other data. These can of course be overridden in the
usual way.

I now have an application (the old "todo" standby) working with ajax calls
to a REST API. (The API was originally developed for an Ember
implementation, so it's at least vaguely realistic.) Sample ajax calls from
this application look reasonable to me:

retrieve = do
r <- ajax ajaxUrl () def
...

update t = do
r <- ajax (ajaxUrlTodo t) (toJSON t) def { asMethod = PUT }
...


Reply to this email directly or view it on GitHub
#5 (comment).

@TobyGoodwin
Copy link
Author

Yes, you're right. (I did say it was ugly!) That's tidied up now. I've made GET the default method, as it was before (and is in jQuery, and in any case is the "safest" default). Also, we no longer need the ToJSRef instance for AjaxSettings, nor the ToJSString instance for Method--show works fine (this isn't considered poor style, is it?)

And then peering at the diff, I noticed that I was ignoring asIfModified and asCache (and using the silly name type, when any jQuery we care about understands method just as well). So the second commit sorts those out.

Hmm... should we allow HEAD as a method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants