You passed the serialized string into the function, which won't work.
(It'll iterate through the characters, as you can see.)

There isn't a Prototype function that collects a form's values into an
object, but it's pretty simple to write one:

function to_object(form) {
  return $(form).getElements().inject({}, function(object, element) {
    o[e.name] = $F(e);
    return o;
  });
}

This doesn't handle multiple valued elements like checkboxes and
multiselects, but it gives the basic idea.  You'd then pass the result of
that to to_exploded_object() to handle the rest:

var object = to_exploded_object(to_object('form_test'));

I might refactor this a bit into a single function.  I think it has a pretty
important use-case: where you want to submit a JSON request directly from a
form in HTML/JS without going through a server-side framework that handles
this unpacking for you.

-Fred

On Thu, Jun 12, 2008 at 7:43 AM, TAOS <[EMAIL PROTECTED]> wrote:

>
> How to use this?
>
> json = to_exploded_object( $('form_test').serialize() );
>
> or
>
> json = to_exploded_object( Object.toJSON( $
> ('form_test').serialize() ) );
>
> Look the result in both examples, this don't work like i expected, but
> i get the ideia to don't use eval. thx both.
>
> {  0:""",   1:"c",   2:"o",   3:"n",   4:"c",   5:"e",   6:"s",
> 7:"s",   8:"i",   9:"o",   10:"n",   11:"a",   12:"r",   13:"i",
> 14:"a",   15:".",   16:"c",   17:"a",   18:"r",   19:"r",
>  20:"o",   21:".",   22:"m",   23:"a",   24:"r",   25:"c",
> 26:"a",   27:".",   28:"c",   29:"o",   30:"d",   31:"i",   32:"g",
> 33:"o",   34:"=",   35:"2",   36:"0",   37:"&",
>  38:"c",   39:"o",   40:"n",   41:"c",   42:"e",   43:"s",
> 44:"s",   45:"i",  ....
>
>
>
> On 11 jun, 22:48, "Frederick Polgardy" <[EMAIL PROTECTED]> wrote:
> > Right, all the uses of eval were unnecessary.
> >
> > I came up with a quick utility along these lines, that allows you to pass
> in
> > an object like:
> >
> > {"a.b.c": 1, "a.b.d":2, "a.b.e[0].f": 3, "a.b.e[1].g": 4}
> >
> > Which you might get from a Prototype form utility function, and get back
> an
> > exploded object like:
> >
> > {"a": {"b": {"c":1, "d":2, "e": [{"f": 3}, {"g": 4}]}}}
> >
> > Which is suitable for passing to Object.toJSON().  It basically just
> handles
> > intermediate objects that can be ordinary values or arrays, but that
> handles
> > all the cases I can think of.  Let me know what you think.
> >
> >   function to_exploded_object(object) {
> >     var root = {};
> >
> >     $H(object).each(function(property) {
> >       var current = root,
> >           path = property.key.split("."),
> >           last = path.pop();
> >
> >       function set_and_advance_key(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) { set_and_advance_key(key, {}); });
> >       set_and_advance_key(last, property.value);
> >     });
> >
> >     return root;
> >   }
> >
> > -Fred
> >
> > On Wed, Jun 11, 2008 at 4:43 PM, kangax <[EMAIL PROTECTED]> wrote:
> >
> > > Fair enough, but still makes little sense to use eval:
> >
> > > function toObject(str) {
> > >  var result = { };
> > >  str.split('.').inject(result, function(parent, child) {
> > >    return parent[child] = parent[child] || { };
> > >  });
> > >  return result;
> > > };
> >
> > > toObject('foo.bar.baz'); // { foo: { bar: baz: { }}}
> >
> > --
> > Science answers questions; philosophy questions answers.
> >
>


-- 
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 rubyonrails-spinoffs@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to