brianleroux / xui

A tiny javascript framework for mobile web apps.
http://github.com/xui/xui
391 stars 159 forks source link

How do I serialize form elements when submitting via xhr? #31

Closed ekallevig closed 13 years ago

ekallevig commented 13 years ago

I'm trying to submit a form with an .xhr call, but I need to serialize my form elements... what's the easiest way to do that? I see a private method called _toQueryString that seems to do that but I'm not sure how to make use of it? Does .xhr use it automatically somehow that I'm not seeing?

SlexAxton commented 13 years ago

This isn't an issue, since xui doesn't really attempt much of this...

It's pretty simple code to write for your application...

var data = {}; x$('form input').each(function(elem){ data[elem.name] = elem.value; });

var stringData = JSON.stringify(data);

// send that string along...

ekallevig commented 13 years ago

I just thought this was already built into the library here: http://github.com/brianleroux/xui/blob/master/src/more/form.js

Isn't that what that function is doing? How is that _toQueryString function used or meant to be used? Why is it private?

Also, I realized this seems to work:

x$()._toQueryString(x$('form#postComment')[0]);
SlexAxton commented 13 years ago

it's a private function, and it builds a querystring.

If you are 'POST'ing the form like you say you are, it might get a little awkward, but I'm sure you could get it to work...

ekallevig commented 13 years ago

Sorry if I'm not explaining myself properly. The larger libraries have functions that serialize form elements (ie. http://api.jquery.com/serialize/) It seems like the private _toQueryString function in XUI does the same thing -- is there a reason it's private? What query strings is it used to build internally?