Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="//jashkenas.github.io/underscore/underscore-min.js"></script>
<script src="//jashkenas.github.io/backbone/backbone-min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
function print(str) {
  document.body.innerHTML += "<pre>" + str + "</pre>";
}
var Model = Backbone.Model.extend({
  defaults: {
    prop: null,
  }
});
var Collection = Backbone.Collection.extend({
  model: Model
});
var collection = new Collection([{prop: "A"}, {prop: "B"}, {prop: "A"}]);
collection.on("add", function(model, collection, params) {
    if (!params.merge) {
        if (collection.where({ prop: model.get("prop") }).length > 1) {
            collection.remove(model);
        }
    }   
});
collection.add({ prop: "B" });
collection.add({ prop: "C" });
print(JSON.stringify(collection.toJSON()));
collection.add({prop: "B"}, {merge: true});
print(JSON.stringify(collection.toJSON()));
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers