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,
  
  _add: function(model, params) {   
    if (!params || !params.merge) {
        if (!this.where(model).length) this.add(model, params);
    } else {
        this.add(model, params);
    }
    return this;
  }
});
var collection = new Collection([{prop: "A"}, {prop: "B"}, {prop: "A"}]);
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