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

Avoid accessing destroyed contexts in loop #55

Merged
merged 5 commits into from
Sep 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backbone.geppetto.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@
Geppetto.Context.prototype.dispatchGlobally = function dispatchGlobally(eventName, eventData) {

_.each(contexts, function(context) {
if (!context) {
return true;
}
context.vent.trigger(eventName, eventData);
});
};
Expand Down
29 changes: 29 additions & 0 deletions specs/src/geppetto-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,35 @@ define([
expect(spy3.callCount).to.equal(1);
});

it("should skip any context that have been destroyed when looping through list of all contexts to trigger event on each", function() {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
var spy3 = sinon.spy();

var spyDispatchGlobally = sinon.spy(context1, 'dispatchGlobally');

context1.listen(view1, "foo", function(){
context2.destroy();
});

context1.listen(view1, "foo", spy1);
context2.listen(view2, "foo", spy2);
context3.listen(view3, "foo", spy3);

expect(spy1.callCount).to.equal(0);
expect(spy2.callCount).to.equal(0);
expect(spy3.callCount).to.equal(0);

context1.dispatchGlobally('foo');

expect(spy1.callCount).to.equal(1);
expect(spy2.callCount).to.equal(0);
expect(spy3.callCount).to.equal(1);

expect(spyDispatchGlobally.threw()).to.equal(false);

});

});

describe("when debug mode is enabled", function() {
Expand Down