icereval/backbone-documentmodel

Strange output on nested collection models toJSON()

azpublic opened this issue · 1 comments

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() .

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