Skip to content

Commit

Permalink
Merge pull request #55 from mmikeyy/patch-2
Browse files Browse the repository at this point in the history
Avoid accessing destroyed contexts in loop
  • Loading branch information
geekdave committed Sep 1, 2014
2 parents 7141879 + d1c94d1 commit 79e3c31
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
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

0 comments on commit 79e3c31

Please sign in to comment.