I think I understand what you're trying to do - you want to take form elements in the form of "a.b.c.d" and generate the correct nested JSON object. Since I have a soft spot for recursive algorithms, I'll probably try to implement this on the way home on the train. ;-) But while the underlying structure will be different, there still isn't a good reason to use eval(). Not just because it's slower, but because it's a lot harder to comprehend. All the expressions you're trying to build up and eval() can be expressed with bracket notation and, possibly, string concatenation to build up property strings that refer to form elements.
I'll play with this a little tonight. -Fred On Wed, Jun 11, 2008 at 4:04 PM, TAOS <[EMAIL PROTECTED]> wrote: > > Is not same. > > i need to break in each point. > > someObject['person.address.phone.something']['property'] = 5 will > generate > > { 'person.address.phone.something': { 'property': 5 } } > > using eval will generate > > { person: { address: { phone: { something: { property: 5 } } } } } > > this is the diference. > > On 11 jun, 17:23, kangax <[EMAIL PROTECTED]> wrote: > > In this case, iterating twice simply removes duplication. The method > > itself is barely performance "critical" (few item collections with not > > too "heavy" calculations). Not repeating yourself is, imo, critical. > > As far as "eval" goes, you seem to be confused about property access: > > > > eval("result." + tree + "." + processkey + ".push(value)"); > > // is "same" as > > result[tree][processkey].push(value); > > > > - kangax > > > > On Jun 11, 3:56 pm, TAOS <[EMAIL PROTECTED]> wrote: > > > > > kangax, > > > > > why i will iterate something twice? if in a iterate i can do two > > > things? > > > > > Is more easy i use a prefix in my adpted method to works like this $ > > > ('form_test').serialize() > > > > > of course the method need get better the code, but its is more useful > > > then the original. > > > > > sorry to my bad english. > > > > > On 11 jun, 16:12, kangax <[EMAIL PROTECTED]> wrote: > > > > > > This looks like an edge case to me ;) > > > > Prototype.js can accomplish similar conversion quite easily: > > > > > > Object.toJSON($('someForm').serialize()); > > > > > > Besides that, I don't see a reason to duplicate half of > Form#serialize > > > > in here. Why not simply iterate over a resulting object? > > > > > > - kangax > > > > > > On Jun 11, 2: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 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 -~----------~----~----~----~------~----~------~--~---