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

Allow the data argument to be a string #26

Open
joshuajabbour opened this issue Apr 13, 2014 · 4 comments
Open

Allow the data argument to be a string #26

joshuajabbour opened this issue Apr 13, 2014 · 4 comments

Comments

@joshuajabbour
Copy link

Since data must be passed as an object, there is no easy way to get form data and pass it into the ajax request (say on a form submission, with data using jQuery.serialize()). The magic arguments parsing in Resource.prototype.extractUrlData seems to make this impossible to fix.

Anyway, I'm not sure how to get around this, but form submission seems like a fairly common rest api necessity.

@joshuajabbour
Copy link
Author

Something like this should be used in case someone else is trying to get around this issue: https://github.com/danheberden/jquery-serializeForm

@jpillora
Copy link
Owner

Edit - $(this).serialize(); returns a string so you must use plugin mentioned above.

$('form').on('submit', function() {
  var obj = $(this).serializeJSON(); //using aforementioned plugin
  client.api.foo.update(obj).done(...);
  return false;
});

@joshuajabbour
Copy link
Author

Did you try your example? It definitely doesn't work, which is why I opened this issue.

As I said, Resource.prototype.extractUrlData only accepts an object for data, so you can't use an encoded string (such as the one you get with jQuery.serialize() or it will get treated as params and added to the query string (it could also get treated as an ID if you didn't provide the requisite number).

And yes, as I mentioned, there are plenty of plugins to help with this, some of which are here in this list. But the user has to realize they need this. I spent a considerable amount of time today trying to figure out what wasn't working, as sending form data is a core use case for ajax integration. I eventually figured it out by stepping through the source code (which was more difficult than it needed to be due to the overly-verbose coffeescript generated output).

Anyway, at the very least, please document this limitation.

@jpillora
Copy link
Owner

Ah yep misread your initial comment, my mistake. data should indeed be allowed to be a string, though it would be difficult due to the current overloading magic. Since there are N id strings. As a simple fix, we could add more magic: {$value: foo} silently becomes foo. With a bit of work, we could add a real fix. Since:

create - POST /foo
read - GET /foo/:id OR GET /foo
update - PUT /foo/:id
delete - delete /foo/:id

Only read needs optional argument, for the other verbs, the number of arguments required will be known. So for non-GETs, instead of type checking we could count arguments: Grab N ids, then data, then queryparams.

Only thing is I don't have time to make this change, so I'll reopen and mark for PR :) Also see #27

@jpillora jpillora reopened this Apr 13, 2014
@jpillora jpillora changed the title Passing data as serialized string from forms Allow the data argument to be a string Apr 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants