You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
when trying to set value for deep nested attribute of a nested model with same old value, change events still fire on parents level, no change event fire on deep attributed level
below test code, with proposed fix
(function(){varBugModel=Backbone.NestedModel.extend({});varFixModel=Backbone.NestedModel.extend({_setAttr: function(newAttrs,attrPath,newValue,opts){opts=opts||{};varfullPathLength=attrPath.length;varmodel=this;varoldVal=Backbone.NestedModel.walkThenGet(newAttrs,attrPath);// See if this is a new value being setvarisNewValue=!_.isEqual(oldVal,newValue);Backbone.NestedModel.walkPath(newAttrs,attrPath,function(val,path,next){varattr=_.last(path);varattrStr=Backbone.NestedModel.createAttrStr(path);if(path.length===fullPathLength){// reached the attribute to be setif(opts.unset){// unset the valuedeleteval[attr];// Trigger Remove Event if array being set to nullif(_.isArray(val)){varparentPath=Backbone.NestedModel.createAttrStr(_.initial(attrPath));model._delayedTrigger('remove:'+parentPath,model,val[attr]);}}else{// Set the new valueval[attr]=newValue;}// Trigger Change Event if new values are being setif(!opts.silent&&_.isObject(newValue)&&isNewValue){varvisited=[];varcheckChanges=function(obj,prefix){// Don't choke on circular referencesif(_.indexOf(visited,obj)>-1){return;}else{visited.push(obj);}varnestedAttr,nestedVal;for(varainobj){if(obj.hasOwnProperty(a)){nestedAttr=prefix+'.'+a;nestedVal=obj[a];if(!_.isEqual(model.get(nestedAttr),nestedVal)){model._delayedChange(nestedAttr,nestedVal,opts);}if(_.isObject(nestedVal)){checkChanges(nestedVal,nestedAttr);}}}};checkChanges(newValue,attrStr);}}elseif(!val[attr]){if(_.isNumber(next)){val[attr]=[];}else{val[attr]={};}}if(!opts.silent){// let the superclass handle change events for top-level attributesif(path.length>1&&isNewValue){model._delayedChange(attrStr,val[attr],opts);}if(_.isArray(val[attr])){model._delayedTrigger('add:'+attrStr,model,val[attr]);}}});}});$(document).ready(function(){varbugModel=newBugModel({Category: {categoryId: 1,categoryName: "Category 1",Types: [{typeId: 1,typeName: "type1"},{typeId: 2,typeName: "type2"}]}});bugModel.on("change:Category.Types",function(){console.log("no actual change but bug parent change event fired");});bugModel.on("change:Category.Types[0].typeName",function(){console.log("fire only with correct change");});varfixModel=newFixModel({Category: {categoryId: 1,categoryName: "Category 1",Types: [{typeId: 1,typeName: "type1"},{typeId: 2,typeName: "type2"}]}});fixModel.on("change:Category.Types",function(){console.log("fix model have changed");});bugModel.set('Category.Types[0].typeName',"type1");fixModel.set('Category.Types[0].typeName',"type1");});})();
The text was updated successfully, but these errors were encountered:
when trying to set value for deep nested attribute of a nested model with same old value, change events still fire on parents level, no change event fire on deep attributed level
below test code, with proposed fix
The text was updated successfully, but these errors were encountered: