Skip to content

Commit

Permalink
Fixing un-released models from refresh'd collections. Issue #128
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Dec 8, 2010
1 parent 784adc6 commit 6ea500b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
14 changes: 10 additions & 4 deletions backbone.js
Expand Up @@ -388,7 +388,7 @@
this.comparator = options.comparator;
delete options.comparator;
}
this._boundOnModelEvent = _.bind(this._onModelEvent, this);
_.bindAll(this, '_onModelEvent', '_removeReference');
this._reset();
if (models) this.refresh(models, {silent: true});
this.initialize(models, options);
Expand Down Expand Up @@ -474,6 +474,7 @@
refresh : function(models, options) {
models || (models = []);
options || (options = {});
this.each(this._removeReference);
this._reset();
this.add(models, {silent: true});
if (!options.silent) this.trigger('refresh', this, options);
Expand Down Expand Up @@ -547,7 +548,7 @@
model.collection = this;
var index = this.comparator ? this.sortedIndex(model, this.comparator) : this.length;
this.models.splice(index, 0, model);
model.bind('all', this._boundOnModelEvent);
model.bind('all', this._onModelEvent);
this.length++;
if (!options.silent) model.trigger('add', model, this, options);
return model;
Expand All @@ -561,14 +562,19 @@
if (!model) return null;
delete this._byId[model.id];
delete this._byCid[model.cid];
delete model.collection;
this.models.splice(this.indexOf(model), 1);
this.length--;
if (!options.silent) model.trigger('remove', model, this, options);
model.unbind('all', this._boundOnModelEvent);
this._removeReference(model);
return model;
},

// Internal method to remove a model's ties to a collection.
_removeReference : function(model) {
delete model.collection;
model.unbind('all', this._onModelEvent);
},

// Internal method called every time a model in the set fires an event.
// Sets need to update their indexes when models change ids. All other
// events simply proxy through. "add" and "remove" events that originate
Expand Down
13 changes: 13 additions & 0 deletions test/collection.js
Expand Up @@ -84,6 +84,19 @@ $(document).ready(function() {
equals(otherRemoved, null);
});

test("Collection: events are unbound on remove", function() {
var counter = 0;
var dj = new Backbone.Model();
var emcees = new Backbone.Collection([dj]);
emcees.bind('change', function(){ counter++; });
dj.set({name : 'Kool'});
equals(counter, 1);
emcees.refresh([]);
equals(dj.collection, undefined);
dj.set({name : 'Shadow'});
equals(counter, 1);
});

test("Collection: remove in multiple collections", function() {
var modelData = {
id : 5,
Expand Down

0 comments on commit 6ea500b

Please sign in to comment.