Skip to content

Commit 4541030

Browse files
paulfalgoutJSteunou
authored andcommitted
Prevent undelegateEntityEvents from over removing (#3528)
Fixes #3527
1 parent fee4030 commit 4541030

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/mixins/delegate-entity-events.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@ import {
1212
export default {
1313
// Handle `modelEvents`, and `collectionEvents` configuration
1414
_delegateEntityEvents(model, collection) {
15-
this._undelegateEntityEvents(model, collection);
16-
1715
const modelEvents = _.result(this, 'modelEvents');
18-
bindEvents.call(this, model, modelEvents);
16+
17+
if (modelEvents) {
18+
unbindEvents.call(this, model, modelEvents);
19+
bindEvents.call(this, model, modelEvents);
20+
}
21+
1922

2023
const collectionEvents = _.result(this, 'collectionEvents');
21-
bindEvents.call(this, collection, collectionEvents);
24+
25+
if (collectionEvents) {
26+
unbindEvents.call(this, collection, collectionEvents);
27+
bindEvents.call(this, collection, collectionEvents);
28+
}
2229
},
2330

2431
_undelegateEntityEvents(model, collection) {

test/unit/view.entity-events.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,25 @@ describe('view entity events', function() {
217217
expect(this.barStub).to.have.been.calledOnce;
218218
});
219219
});
220+
221+
// Fixes https://github.com/marionettejs/backbone.marionette/issues/3527
222+
describe('when entity events are added in initialize', function() {
223+
it('should not undelegate them', function() {
224+
const View = Marionette.View.extend({
225+
template: false,
226+
initialize() {
227+
this.listenTo(this.model, 'foo', this.onFoo);
228+
},
229+
onFoo: this.sinon.stub()
230+
});
231+
232+
const model = new Backbone.Model();
233+
234+
const view = new View({ model });
235+
236+
model.trigger('foo');
237+
238+
expect(view.onFoo).to.have.been.calledOnce;
239+
});
240+
});
220241
});

0 commit comments

Comments
 (0)