addyosmani / backbone-fundamentals

:book: A creative-commons book on Backbone.js for beginners and advanced users alike

Home Page:http://addyosmani.github.io/backbone-fundamentals/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Collections {merge: true} unclear

adamzerner opened this issue · comments

Also note that when using add() on a collection, passing {merge: true} causes duplicate models to have their attributes merged in to the existing models, instead of being ignored.

var items = new Backbone.Collection;
items.add([{ id : 1, name: "Dog" , age: 3}, { id : 2, name: "cat" , age: 2}]);
items.add([{ id : 1, name: "Bear" }], {merge: true });
items.add([{ id : 2, name: "lion" }]); // merge: false

console.log(JSON.stringify(items.toJSON()));
// [{"id":1,"name":"Bear","age":3},{"id":2,"name":"cat","age":2}]

What exactly is a "duplicate model"? It seems to be where they have the same id, but I'm not sure.

And does items.add([{ id : 2, name: "lion" }]); // merge: false log out some sort of error?