Comparing version
@@ -541,1 +541,44 @@ /* jshint asi:true */ | ||
}); | ||
QUnit.test(".attr(props) should overwrite if _legacyAttrBehavior is true (#112)", function(){ | ||
Map.prototype._legacyAttrBehavior = true; | ||
var myMap1Instance = new Map({prop1: new Map()}); | ||
var changes = 0; | ||
myMap1Instance.on("prop1", function() { | ||
changes++; | ||
}); | ||
var map2 = new Map({prop1: "xyz"}); | ||
myMap1Instance.attr({ | ||
"prop1": map2 | ||
}); | ||
delete Map.prototype._legacyAttrBehavior; | ||
QUnit.equal(changes,1, "caused a change event"); | ||
QUnit.equal(myMap1Instance.attr("prop1"), map2, "overwrite with maps"); | ||
}); | ||
QUnit.test(".attr() leaves typed instances alone if _legacyAttrBehavior is true (#111)", function(){ | ||
Map.prototype._legacyAttrBehavior = true; | ||
function MyClass(value){ | ||
this.value = value; | ||
} | ||
MyClass.prototype.log = function(){ | ||
return this.value; | ||
}; | ||
var myMap = new Map({ | ||
myClass: new MyClass(5) | ||
}); | ||
QUnit.equal( myMap.attr().myClass, myMap.attr("myClass") ) | ||
delete Map.prototype._legacyAttrBehavior; | ||
}); |
@@ -481,3 +481,8 @@ "use strict"; | ||
_getAttrs: function(){ | ||
return canReflect.unwrap(this, CIDMap); | ||
if(this._legacyAttrBehavior) { | ||
return mapHelpers.serialize(this, 'attr', {}); | ||
} else { | ||
return canReflect.unwrap(this, CIDMap); | ||
} | ||
}, | ||
@@ -490,2 +495,5 @@ // ### _setAttrs | ||
_setAttrs: function (props, remove) { | ||
if(this._legacyAttrBehavior) { | ||
return this.__setAttrs(props, remove); | ||
} | ||
if(remove === true || remove === "true") { | ||
@@ -498,3 +506,53 @@ this[canSymbol.for("can.updateDeep")](props); | ||
}, | ||
__setAttrs: function (props, remove) { | ||
props = assign({}, props); | ||
var prop, | ||
self = this, | ||
newVal; | ||
// Batch all of the change events until we are done. | ||
canQueues.batch.start(); | ||
// Merge current properties with the new ones. | ||
this._each(function (curVal, prop) { | ||
// You can not have a _cid property; abort. | ||
if (prop === "_cid") { | ||
return; | ||
} | ||
newVal = props[prop]; | ||
// If we are merging, remove the property if it has no value. | ||
if (newVal === undefined) { | ||
if (remove) { | ||
self.removeAttr(prop); | ||
} | ||
return; | ||
} | ||
// Run converter if there is one. Remove in 3.0. | ||
if (self.__convert) { | ||
newVal = self.__convert( prop, newVal ); | ||
} | ||
if ( types.isMapLike(curVal) && mapHelpers.canMakeObserve(newVal) ) { | ||
curVal.attr(newVal, remove); | ||
// Otherwise just set. | ||
} else if (curVal !== newVal) { | ||
self.__set(prop, self.__type(newVal, prop), curVal); | ||
} | ||
delete props[prop]; | ||
}); | ||
// Add remaining props. | ||
for (prop in props) { | ||
// Ignore _cid. | ||
if (prop !== "_cid") { | ||
newVal = props[prop]; | ||
this._set(prop, newVal, true); | ||
} | ||
} | ||
canQueues.batch.stop(); | ||
return this; | ||
}, | ||
serialize: function () { | ||
@@ -501,0 +559,0 @@ return canReflect.serialize(this, CIDMap); |
@@ -9,2 +9,3 @@ "use strict"; | ||
var canReflect = require('can-reflect'); | ||
var canSymbol = require("can-symbol"); | ||
// ## POJOs to Map instance helpers | ||
@@ -82,3 +83,3 @@ | ||
// `where` - the target Object or Array that becomes the serialized result. | ||
/*serialize: (function(){ | ||
serialize: (function(){ | ||
@@ -134,3 +135,3 @@ // A temporary mapping of map cids to the serialized result. | ||
}; | ||
})(),*/ | ||
})(), | ||
@@ -140,3 +141,3 @@ // ## getValue | ||
// returns the value of `val`. | ||
/*getValue: function(map, name, val, how){ | ||
getValue: function(map, name, val, how){ | ||
if(how === "attr") { | ||
@@ -150,3 +151,3 @@ how = canSymbol.for("can.getValue"); | ||
} | ||
},*/ | ||
}, | ||
@@ -153,0 +154,0 @@ // ## define |
{ | ||
"name": "can-map", | ||
"version": "4.1.2", | ||
"version": "4.2.0", | ||
"description": "Observable Objects", | ||
@@ -5,0 +5,0 @@ "homepage": "http://canjs.com", |
81385
2.78%1613
5.22%24
-7.69%