afeld / backbone-nested

A plugin to make Backbone.js keep track of nested attributes - looking for maintainers! https://github.com/afeld/backbone-nested/issues/157
https://afeld.github.com/backbone-nested/
MIT License
445 stars 83 forks source link

Cannot set property of nested array by index dynamically #127

Open daveed80 opened 9 years ago

daveed80 commented 9 years ago

I'm attempting to set a property of a nested array dynamically but am unable to do so.

Model :

{"Alias":"sample","Id":0, Traits: [ {"AttrValue":null}, {"AttrValue":null} ] }

This of course works fine:

// this.model.set({ 'Traits[0].AttrValue': 'test' });

This does not:

// var i = 2; // var prop = 'Traits[' + i + '].AttrValue'; // this.model.set({' prop ' : 'test'});

I'm sure I just can't see the sailboat at the moment (exhaustion) so any help would be greatly appreciated.

Thanks in advance!

afeld commented 9 years ago

Figure it out?

daveed80 commented 9 years ago

Thanks so much for the reply!

Well, both yes and no...

I just realized I can do:

// var traits = this.model.get('Traits'); // traits[0].AttrValue = 'test'; // this.model.set('Traits', traits);

What I would inevitably like to do is find the 'trait' object from the array by 'AttrName', and set it directly, avoiding needing to know the index and still updating the top level model.

Any suggestions?

daveed80 commented 9 years ago

By the way, excellent work on this!