Here's an updated version of my to_exploded_object function, that takes a
form instead of an object, so you can pass the form in in one shot. It
still doesn't handle multivalued inputs, but it does handle arrays.
function toJSONObject(form) {
var object = {};
Form.getElements(form).each(function(element) {
var current = object,
path = element.name.split("."),
last = path.pop();
function advanceToNextNode(key, value) {
var match = key.match(/^(\w+)(?:\[(\d+)\])?/),
name = match[1],
index = match[2];
if (index) {
index = parseInt(index);
current[name] = current[name] || [];
current[name][index] = current[name][index] || value;
current = current[name][index];
} else {
current[name] = current[name] || value;
current = current[name];
}
}
path.each(function(key) { advanceToNextNode(key, {}); });
advanceToNextNode(last, Form.Element.getValue(element));
});
return object;
}
Object.toJSON(toJSONObject('form_test'));
(The function still returns an object, not a JSON string, because there are
a lot of use-cases for keeping it in that form as well.)
-Fred
--
Science answers questions; philosophy questions answers.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---