mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-08 03:00:26 -04:00
fix bug in 'add' and 'refresh'
This commit is contained in:
18
backbone.js
18
backbone.js
@@ -284,9 +284,16 @@
|
||||
// Provides a standard collection class for our sets of models, ordered
|
||||
// or unordered. If a `comparator` is specified, the Collection will maintain
|
||||
// its models in sort order, as they're added and removed.
|
||||
Backbone.Collection = function(options) {
|
||||
Backbone.Collection = function(models,options) {
|
||||
if (options && options.comparator) {
|
||||
this.comparator = options.comparator;
|
||||
delete options.comparator;
|
||||
}
|
||||
this._boundOnModelEvent = _.bind(this._onModelEvent, this);
|
||||
this._initialize();
|
||||
if (models) {
|
||||
this.refresh(models,true);
|
||||
}
|
||||
};
|
||||
|
||||
// Define the Collection's inheritable methods.
|
||||
@@ -340,7 +347,7 @@
|
||||
if (already) throw new Error(["Can't add the same model to a set twice", already.id]);
|
||||
this._byId[model.id] = model;
|
||||
this._byCid[model.cid] = model;
|
||||
var index = this.comparator ? this.sortedIndex(model, this.comparator) : this.length - 1;
|
||||
var index = this.comparator ? this.sortedIndex(model, this.comparator) : this.length;
|
||||
this.models.splice(index, 0, model);
|
||||
model.bind('all', this._boundOnModelEvent);
|
||||
this.length++;
|
||||
@@ -375,10 +382,9 @@
|
||||
refresh : function(models, silent) {
|
||||
models = models || [];
|
||||
if (models[0] && !(models[0] instanceof Backbone.Model)) {
|
||||
for (var i = 0, l = models.length; i < l; i++) {
|
||||
models[i].collection = this;
|
||||
models[i] = new this.model(models[i]);
|
||||
}
|
||||
_.each(models, _.bind(function(model,i) {
|
||||
model.collection = this;
|
||||
}, this));
|
||||
}
|
||||
this._initialize();
|
||||
this.add(models, true);
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
$(document).ready(function() {
|
||||
|
||||
module("Backbone collections");
|
||||
|
||||
test("collections: simple", function() {
|
||||
a = new Backbone.Model({label: 'a'});
|
||||
b = new Backbone.Model({label: 'b'});
|
||||
c = new Backbone.Model({label: 'c'});
|
||||
d = new Backbone.Model({label: 'd'});
|
||||
col = new Backbone.Collection([a,b,c,d]);
|
||||
equals(col.first(),a, "a should be first");
|
||||
equals(col.last(),d, "d should be last");
|
||||
});
|
||||
|
||||
test("collections: sorted", function() {
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
@@ -27,7 +27,9 @@ $(document).ready(function() {
|
||||
test("model: isNew", function() {
|
||||
attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
|
||||
a = new Backbone.Model(attrs);
|
||||
ok(a.isNew());
|
||||
ok(a.isNew(), "it should be new");
|
||||
attrs = { 'foo': 1, 'bar': 2, 'baz': 3, 'id': -5 };
|
||||
ok(a.isNew(), "any defined ID is legal, negative or positive");
|
||||
})
|
||||
|
||||
test("model: set", function() {
|
||||
@@ -38,6 +40,13 @@ $(document).ready(function() {
|
||||
a.set({'foo': 2});
|
||||
ok(a.get('foo')==2, "Foo should have changed.");
|
||||
ok(changeCount == 1, "Change count should have incremented.");
|
||||
a.set({'foo': 2}); // set with value that is not new shouldn't fire change event
|
||||
ok(a.get('foo')==2, "Foo should NOT have changed, still 2");
|
||||
ok(changeCount == 1, "Change count should NOT have incremented.");
|
||||
|
||||
a.unset('foo');
|
||||
ok(a.get('foo')==null, "Foo should have changed");
|
||||
ok(changeCount == 2, "Change count should have incremented for unset.");
|
||||
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user