icereval / backbone-documentmodel

A plugin to create entire Document structures with nested Backbone.js Models & Collections with deep model references and event bubbling.
MIT License
66 stars 8 forks source link

Strange output on nested collection models toJSON() #7

Closed azpublic closed 10 years ago

azpublic commented 10 years ago

I encountered what I believe is a nasty edge case when calling toJSON when iterating on the models of a nested collection.


//TEST

                    console.error("_________________________________________________");
var fakeLocRaw = {
    name : 'footestLoc',
    emails : ['foo@bar.com', 'whiz@foo.foo', 'haha@lol.joke']
};

var loc = new Backbone.DocumentModel(fakeLocRaw);

console.error(loc);
console.error(loc.toJSON());

console.error("loc.get('emails') = ");
console.error(loc.get('emails'));

var emlJSON = loc.get('emails').toJSON();
console.log(emlJSON);

loc.get('emails').each(function(eml){
    console.error("eml = ");
    console.error(eml.toJSON());
});

output :


_________________________________________________ 
Backbone.Model.extend.constructor {cid: "c5112", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…}

Object {name: "footestLoc", emails: Array[3]}

loc.get('emails') =
Backbone.Collection.extend.constructor {idAttribute: "id", parent: Backbone.Model.extend.constructor, name: "emails", length: 3, models: Array[3]…}

["foo@bar.com", "whiz@foo.foo", "haha@lol.joke", remove: function, removeObj: function, pushArray: function, move: function]

eml =  
Object {id: "5113", value: "foo@bar.com"}
eml =  
Object {id: "5115", value: "whiz@foo.foo"}
eml =  
Object {id: "5117", value: "haha@lol.joke"}
_________________________________________________ 

Notice how the id attribute is added to the email'JSON when we are iterating over the emails collection with the 'each' method and how that does not occur if calling loc.get('emails').toJSON() .

icereval commented 10 years ago

Thanks for the bug report, should be fixed in the newest release.