Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2: Model events during "remove" do not trigger on Col #3806

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 3 additions & 9 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1102,20 +1102,14 @@
var index = this.indexOf(model);
this.models.splice(index, 1);
this.length--;

// Remove references before triggering 'remove' event to prevent an
// infinite loop. #3693
delete this._byId[model.cid];
var id = this.modelId(model.attributes);
if (id != null) delete this._byId[id];
this._removeReference(model, options);
removed.push(model);

if (!options.silent) {
options.index = index;
model.trigger('remove', model, this, options);
this._onModelEvent('remove', model, this, options);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why call _onModelEvent here? We know it will bypass all of the conditionals because we don't specifically handle remove.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it could be overridden. We're not exactly clear on what is overridable and what's not. #3703

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:eyeroll:

The ones that are meant to be overridden are usually obvious, or their
utility becomes clear when you need them. They don't get in your way if you
don't need them.

I can't think of a reason why you'd override _onModelEvent. It seems to be
there mostly to make it easier to unbind later.

Theres no issue using _onModelEvent but I like the symmetry of the two
triggers in a row.

}

removed.push(model);
this._removeReference(model, options);
}
return removed.length ? removed : false;
},
Expand Down
4 changes: 4 additions & 0 deletions test/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,13 @@
var result = null;
col.on('remove', function(model, col, options) {
removed = model.get('label');
model.trigger('other');
equal(options.index, 3);
equal(col.get(model), undefined, '#3693: model cannot be fetched from collection');
});
col.on('other', function() {
ok(false, 'Events triggered on model during "remove" listener should not be triggered on collection');
});
result = col.remove(d);
equal(removed, 'd');
strictEqual(result, d);
Expand Down