Skip to content

The event argument is not passed for listners attached to a JS object #102

Open

Description

@bartsidee

events attached to DOM object pass back an event argument

bean.on(element, 'click', function (e) {
   console.log(e);
});

but events attached to a JS objects do not receive the event argument

var eventbus = {};
bean.on(eventbus, 'customevent', function (e) {
   console.log(e);
});
bean.trigger(eventbus, 'customevent');

the above outputs undefined.

The implication is that:

  • the .on syntax is not following a standard pattern
  • it is impossible to determine the event type afterwards, e.g. when attaching multiple custom events at the same time this could be useful

.

var eventbus = {};
bean.on(eventbus, 'customevent1 customevent2 customevent3 customevent4', function (e) {
    console.log(e.type);
});
bean.trigger(eventbus, 'customevent1');

Activity

  1. bartsidee commented on Jan 28, 2014

    @bartsidee
    ContributorAuthor

    A quick solution I just found was to pass the handler as event argument to the event

    From https://github.com/fat/bean/blob/master/src/bean.js#L667:

            // non-native event, either because of a namespace, arguments or a non DOM element
            // iterate over all listeners and manually 'fire'
            handlers = registry.get(element, type, null, false)
            args = [false].concat(args)
            for (j = 0, l = handlers.length; j < l; j++) {
              if (handlers[j].inNamespaces(names)) {
                handlers[j].handler.apply(element, args)
              }
            }
    

    To:

            // non-native event, either because of a namespace, arguments or a non DOM element
            // iterate over all listeners and manually 'fire'
            handlers = registry.get(element, type, null, false)
            for (j = 0, l = handlers.length; j < l; j++) {
              if (handlers[j].inNamespaces(names)) {
                handlers[j].handler.apply(element, [false, handlers[j]].concat(args))
              }
            }
    
  2. ryanve commented on Jan 28, 2014

    @ryanve

    Duplicate of #80 Yes we should fix this.

  3. rvagg commented on Jan 28, 2014

    @rvagg
    Collaborator

    @ryanve let's just get this done, want to do up a PR to make the needed changes?

  4. ded commented on Jan 28, 2014

    @ded
    Collaborator

    interesting that this only comes up now. what would one do with an event object from a custom event?

  5. ryanve commented on Jan 28, 2014

    @ryanve

    @rvagg I can tackle this within the next week or so.

    @ded Not lots but including it would help normalize the handler signature between all events: native, custom, triggers.

    For custom events, supporting .type could suffice.

  6. ded commented on Jan 28, 2014

    @ded
    Collaborator
    For custom events, supporting `.type` could suffice.
    

    that's actually a great idea and would work for a first pass. we should probably bump up a minor version after this is implemented

  7. ryanve commented on Jan 28, 2014

    @ryanve

    Sounds like a plan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions