Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maximum Call Stack Exceeded When Destroying a nested model #476

Merged
merged 3 commits into from Dec 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion bower.json
Expand Up @@ -10,6 +10,7 @@
],
"license": "MIT",
"devDependencies": {
"qunit": "~1.12.0"
"qunit": "~1.12.0",
"zepto": "~1.0.0"
}
}
10 changes: 9 additions & 1 deletion map/attributes/attributes.js
@@ -1,4 +1,4 @@
steal('can/util', 'can/map', function(can, Map) {
steal('can/util', 'can/map', 'can/list', function(can, Map) {
can.each([ can.Map, can.Model ], function(clss){
// in some cases model might not be defined quite yet.
if(clss === undefined){
Expand Down Expand Up @@ -346,6 +346,10 @@ can.Map.prototype.__convert = function(prop, value){
*
* contact.serialize('birthday') //-> 'YYYY-MM-DD'
*/
can.List.prototype.serialize = function(attrName, stack) {
return can.makeArray(can.Map.prototype.serialize.apply(this, arguments));
}

can.Map.prototype.serialize = function(attrName, stack) {
var where = {},
Class = this.constructor,
Expand Down Expand Up @@ -386,6 +390,10 @@ can.Map.prototype.serialize = function(attrName, stack) {
}
});

if(typeof attrs.length !== 'undefined') {
where.length = attrs.length;
}

return attrName != undefined ? where[attrName] : where;
};
return can.Map;
Expand Down
41 changes: 41 additions & 0 deletions map/attributes/attributes_test.js
Expand Up @@ -693,4 +693,45 @@ test("Convert can.Model using .model and .models (#293)", 5, function() {
equal(link.attr('levelsCompleted.1.index'), 1, 'Data ran through Level.models');
});

test('Maximum call stack size exceeded with global models (#476)', function() {
stop();

window.Character = can.Model.extend({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to use attributes / converters without adding to the window.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interesting thing is that it worked when just passing the instances but the stack overflow happens when it is on the window using the string notation (Game.model, Game.models).

attributes: {
game: 'Game.model'
}
}, {});

window.Game = can.Model.extend({
attributes: {
characters: 'Character.models'
},
findOne: function() {
var dfd = can.Deferred();
setTimeout(function() {
dfd.resolve({
"id": "LOZ",
"name": "The Legend of Zelda",
"characters": [{
"id": "1",
"name": "Link",
"game": {
"id": "LOZ"
}
}]
});
}, 100);
return dfd;
}
}, {});

window.Game.findOne({id: 'LOZ'}).then(function(g) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a findOne necessary? Could new be used instead?

ok(g.serialize(), 'No error serializing the object');
start();
});

delete window.Game;
delete window.Character;
});

})();