Skip to content
This repository has been archived by the owner. It is now read-only.

Stapes.on returning invalid e.scope #19

Closed
contra opened this issue Oct 8, 2012 · 6 comments
Closed

Stapes.on returning invalid e.scope #19

contra opened this issue Oct 8, 2012 · 6 comments

Comments

@contra
Copy link

@contra contra commented Oct 8, 2012

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

@hay
Copy link
Owner

@hay hay commented Oct 9, 2012

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...

@hay hay closed this Oct 9, 2012
@hay hay reopened this Oct 9, 2012
@contra
Copy link
Author

@contra contra commented Oct 9, 2012

Are there any existing tests for the events system? Right now I'm only getting the issue with Stapes.on

@contra
Copy link
Author

@contra contra commented Oct 9, 2012

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.

@contra
Copy link
Author

@contra contra commented Oct 9, 2012

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

@hay
Copy link
Owner

@hay hay commented Oct 10, 2012

Thanks for expanding the test and finding the cause. I'll have a look and hopefully can fix it easily.

@hay
Copy link
Owner

@hay hay commented Oct 14, 2012

This issue has been fixed in master. Thanks for your comments and test code!

@hay hay closed this Oct 14, 2012
hay added a commit that referenced this issue Oct 14, 2012
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.