javascript - Changing nested attribute with model.set in Backbone -


i'm trying hope simple thing -- doing model.set on sub attribute of object.

right now, have model looks this:

{             "attr1" : true,             "attr2" : this.model.get("username"),             "attr3" : $('#tenant_select').val(),             "attr_array": [                 {                     "id": "sub_1",                     "state": "active"                 },                 {                     "id": "sub_22",                     "state": "not_active"                 }             ]         } 

i want able grab reach mymode.attr_array.state , change value. however, using .set i've been able change attributes on first level, i.e. attr_array.

is there way using model.set?

you can (i'm wondering why didn't manage it). have careful:

var array = this.get('attr_array'); array[1].state = 'active'; this.set('attr_array', array); 

what's problem here? model holds reference of object. therefore last line useless, won't change @ all. it's equivalent to:

this.get('attr_array')[1].state = 'active'; 

and lose internal stuff backbone when use set.
do? clone object:

var array = _.clone(this.get('attr_array')); array[1].state = 'active'; this.set('attr_array', array); // trigger 'change' , 'change:attr_array' events 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -