Skip to content

Commit

Permalink
Merge pull request canjs#1319 from michalbe/issue-1261-constructors-a…
Browse files Browse the repository at this point in the history
…re-not-methods

[Issue 1261] - constructor passed to the scope should be threated as a property
  • Loading branch information
daffl committed Nov 18, 2014
2 parents 7e0b2cc + ccf9cfb commit cc6428b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
29 changes: 18 additions & 11 deletions map/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ steal('can/util', 'can/util/bind','./bubble.js', 'can/construct', 'can/util/batc
//!steal-remove-end
for (var prop in this.prototype) {
// Non-functions are regular defaults.
if (prop !== "define" && typeof this.prototype[prop] !== "function") {
if (
prop !== "define" &&
prop !== "constructor" &&
(
typeof this.prototype[prop] !== "function" ||
this.prototype[prop].prototype instanceof can.Construct
)
) {
this.defaults[prop] = this.prototype[prop];
// Functions with an `isComputed` property are computes.
} else if (this.prototype[prop].isComputed) {
Expand Down Expand Up @@ -110,7 +117,7 @@ steal('can/util', 'can/util/bind','./bubble.js', 'can/construct', 'can/util/batc
// Parses attribute name into its parts.
attrParts: function (attr, keepKey) {
//Keep key intact

if (keepKey ) {
return [attr];
}
Expand Down Expand Up @@ -186,7 +193,7 @@ steal('can/util', 'can/util/bind','./bubble.js', 'can/construct', 'can/util/batc
firstSerialize = false;
if(!serializeMap) {
firstSerialize = true;
// Serialize might call .attr() so we need to keep different map
// Serialize might call .attr() so we need to keep different map
serializeMap = {
attr: {},
serialize: {}
Expand Down Expand Up @@ -254,7 +261,7 @@ steal('can/util', 'can/util/bind','./bubble.js', 'can/construct', 'can/util/batc
if(obj instanceof can.Map){
obj = obj.serialize();
}

// `_data` is where we keep the properties.
this._data = {};
/**
Expand Down Expand Up @@ -317,11 +324,11 @@ steal('can/util', 'can/util/bind','./bubble.js', 'can/construct', 'can/util/batc
target: ev.target
}, [newVal, oldVal]);


},
// Trigger a change event.
_triggerChange: function (attr, how, newVal, oldVal) {
// so this change can bubble ... a bubbling change triggers the
// so this change can bubble ... a bubbling change triggers the
// _changes trigger
if(bubble.isBubbling(this, "change")) {
can.batch.trigger(this, {
Expand All @@ -331,7 +338,7 @@ steal('can/util', 'can/util/bind','./bubble.js', 'can/construct', 'can/util/batc
} else {
can.batch.trigger(this, attr, [newVal, oldVal]);
}

if(how === "remove" || how === "add") {
can.batch.trigger(this, {
type: "__keys",
Expand Down Expand Up @@ -412,8 +419,8 @@ steal('can/util', 'can/util/bind','./bubble.js', 'can/construct', 'can/util/batc
_get: function (attr) {
attr = ""+attr;
var dotIndex = attr.indexOf('.');


// Handles the case of a key having a `.` in its name
// Otherwise we have to dig deeper into the Map to get the value.
if( dotIndex >= 0 ) {
Expand Down Expand Up @@ -475,9 +482,9 @@ steal('can/util', 'can/util/bind','./bubble.js', 'can/construct', 'can/util/batc
if(!keepKey && dotIndex >= 0){
var first = attr.substr(0, dotIndex),
second = attr.substr(dotIndex+1);

current = this._init ? undefined : this.__get( first );

if( Map.helpers.isObservable(current) ) {
current._set(second, value);
} else {
Expand Down
20 changes: 16 additions & 4 deletions map/map_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ steal("can/map", "can/compute", "can/test", "can/list", function(){

testMap.serialize();



ok(can.inArray("cats", attributesRead ) !== -1 && can.inArray( "dogs", attributesRead ) !== -1, "map serialization triggered __reading on all attributes");
ok(readingTriggeredForKeys, "map serialization triggered __reading for __keys");
Expand Down Expand Up @@ -230,10 +230,10 @@ steal("can/map", "can/compute", "can/test", "can/list", function(){
test("serializing cycles", function(){
var map1 = new can.Map({name: "map1"});
var map2 = new can.Map({name: "map2"});

map1.attr("map2", map2);
map2.attr("map1", map1);

var res = map1.serialize();
equal(res.name, "map1");
equal(res.map2.name, "map2");
Expand Down Expand Up @@ -268,12 +268,24 @@ steal("can/map", "can/compute", "can/test", "can/list", function(){

data.attr('name', 'David');
});

test("map passed to Map constructor (#1166)", function(){
var map = new can.Map({x: 1});
var res = new can.Map(map);
deepEqual(res.attr(), {
x: 1
}, "has the same properties");
});

test("constructor passed to scope is threated as a property (#1261)", function(){
var Constructor = can.Construct.extend({});

var Map = can.Map.extend({
Todo: Constructor
});

var m = new Map();

equal(m.attr("Todo"), Constructor);
});
});

0 comments on commit cc6428b

Please sign in to comment.