Stapes.on returning invalid e.scope #19
Comments
Does this problem occur with all events, or just the global ones coming from Stapes.on? I know it's a problem with the latter one. A testcase would be helpful... |
Are there any existing tests for the events system? Right now I'm only getting the issue with Stapes.on |
I see you added tests for event scope but I extended it a bit test("event scope", function() {
var module1 = Stapes.create();
var module2 = Stapes.create();
var firstDone = false;
module1.on('eventscope', function(data, e) {
ok(e.scope === module1, "Scope of event should be the emitting model");
});
module2.on('eventscope', function(data, e) {
ok(e.scope === module2, "Scope of event should be the emitting model");
});
Stapes.on('eventscope', function(data, e) {
if (firstDone) {
ok(e.scope !== module1, "Scope of event from global Stapes object should not be the mixed with other models");
ok(e.scope === module2, "Scope of event from global Stapes object should be the emitting model");
} else {
ok(e.scope === module1, "Scope of event from global Stapes object should be the emitting model");
firstDone = true;
}
});
Stapes.on('all', function(data, e) {
if (e.type === "eventscope") {
// Prevent other events from other tests getting here
ok(e.scope === module1, "Scope event from on 'all' handler on Stapes.on should be emitting model");
}
});
module1.emit('eventscope');
module2.emit('eventscope');
}); I'm emitting eventscope twice and on the second one the scope is still set to module1 even though it is being emitted from module2. |
It looks like the issue is https://github.com/hay/stapes/blob/master/stapes.js#L271 event.scope = module1 and this = module2 but it prefers event.scope over this. I think the issue is that when module1 gets called you are setting event.scope to the scope of module1 then when module2 gets called its using event.scope over this because it was previously set to this of module1. The problem can be solved by cloning the event object before modifying it emitEvents : function(type, data, explicitType, explicitGuid) {
explicitType = explicitType || false;
explicitGuid = explicitGuid || this._guid;
util.each(_.eventHandlers[explicitGuid][type], function(event) {
var scope = (event.scope) ? event.scope : this;
var nevent = util.clone(event);
if (explicitType) {
nevent.type = explicitType;
}
nevent.scope = scope;
event.handler.call(scope, data, nevent);
}, this);
}, I won't bother you will a pull req because you'll probably want to reimpliment the tests/fix your own way |
Thanks for expanding the test and finding the cause. I'll have a look and hopefully can fix it easily. |
This issue has been fixed in master. Thanks for your comments and test code! |
For some reason e.scope is always the same no matter which module is emitting the event. It seems like after the first emit of the event the scope gets locked and used for all future events
The text was updated successfully, but these errors were encountered: