How can i lose the eval in this case.
The tree's var is a hierach of the iterable key. like
person.address.phone.something ...
I can't do this someObject['person.address.phone.something'], then i
need to use eval
On 11 jun, 15:22, "Frederick Polgardy" <[EMAIL PROTECTED]> wrote:
> Wow, first of all, lose the eval()'s.
>
> You don't need to dynamically construct a call to eval() to resolve a
> property. Instead of:
>
> eval("someObject." + someProperty)
>
> You should say:
>
> someObject[someProperty]
>
> And this is, of course, chainable:
>
> a[property1][property2]
>
> -Fred
>
>
>
> On Wed, Jun 11, 2008 at 1:01 PM, TAOS <[EMAIL PROTECTED]> wrote:
>
> > I try to use the prototype form method called serializeElements, to
> > get all elements in form and build a JSON var.
> > It work, but not in the best way.
> > It method get all fields in a form and build a JSON, but it dont put
> > JSON inside JSON.
> > Example:
>
> > If i have the follow form:
>
> > input name="person.name"
> > input name="person.age"
> > input name="person.address.street"
>
> > The serializeElements's method build a JSON like this
>
> > { "person.name": "??", "person.age": "??", "person.address.street":
> > "??" }
>
> > With my modified method the JSON will be generated like this
>
> > { name: "??", age: "??", address: { street: "??" } }
> > Or
> > { car: { name: "??", age: "??", address: { street: "??" } } }
>
> > What are you think about this method?
>
> > Can i add this method in prototype framework? How?
>
> > /**
> > * Serialize a form into a JSON var
> > *
> > * How to use:
> > *
> > * var json = formToJSON('form_test', { prefix : 'car.', exclude:
> > ['description'] } );
> > *
> > * <form id="form_test">
> > * <input type="text" name="car.id" value="2" />
> > * <input type="text" name="car.name" value="Z5" />
> > * <input type="text" name="car.description" value="Sport car" />
> > * <select name="car.company.id">
> > * <option value="1" selected="selected">BMW</value>
> > * <option value="2">Mercedes</value>
> > * </select>
> > * <input type="checkbox" name="car.optionals" value="carsound"
> > checked="checked">Car Sound
> > * <input type="checkbox" name="car.optionals" value="gps">GPS
> > * <input type="checkbox" name="car.optionals" value="absbreak"
> > checked="checked">ABS Break
> > * </form>
> > *
> > * The example above will generate the folow JSON.
> > *
> > * { id: '2', name: 'Z5', company: { id: 1 }, optionals: ['carsound',
> > 'absbreak'] }
> > */
> > function formToJSON(form, options) {
> > elements = $(form).getElements();
> > if (typeof options != 'object') options = { hash: !!options };
> > else if (Object.isUndefined(options.hash)) options.hash = true;
> > var key, value, submitted = false, submit = options.submit;
>
> > var data = elements.inject({ }, function(result, element) {
> > if (!element.disabled && element.name) {
> > key = element.name;
> > value = $(element).getValue();
> > if (value != null && (element.type != 'submit' ||
> > (!submitted &&
> > submit !== false && (!submit || key == submit) &&
> > (submitted
> > = true)))) {
>
> > key = ( !Object.isUndefined(options.prefix) ) ?
> > key.replace(options.prefix,"") : key;
>
> > if( Object.isArray(options.exclude) )
> > if( options.exclude.indexOf(key) != -1 )
> > return result;
>
> > if( key.indexOf(".") != -1 ) {
> > var processkey = key;
> > var tree = "";
> > while( processkey.indexOf(".") != -1 ) {
> > var newkey = processkey.substring(
> > 0,
> > processkey.indexOf(".") );
> > processkey = processkey.replace(
> > newkey + ".", "" );
>
> > tree += (tree == "") ? newkey : "."
> > + newkey;
>
> > if( eval("result." + tree) ==
> > undefined ||
> > eval("result." + tree) == null )
> > eval("result." + tree + " =
> > new Object()") ;
>
> > if( processkey.indexOf(".")
> > == -1 )
> > if( processkey in
> > eval( "result." + tree ) ) {
> > if (
> > !Object.isArray( eval( "result." + tree + "." +
> > processkey ) ) )
>
> > eval("result." + tree + "." + processkey + " = [ result." +
> > tree + "." + processkey + "]");
>
> > eval("result." + tree + "." + processkey +
> > ".push(value)");
> > } else
> > eval("result." +
> > tree + "." + processkey + " =
> > value");
> > }
> > } else
> > if (key in result) {
> > if ( !Object.isArray( result[key] ) )
> > result[key] = [ result[key] ];
>
> > result[key].push(value);
> > } else
> > result[key] = value;
> > }
> > }
>
> > return result;
> > });
> > return options.hash ? data : Object.toQueryString(data);
> > };
>
> > thx,
> > Thiago Antonius
>
> --
> 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
-~----------~----~----~----~------~----~------~--~---