bean icon indicating copy to clipboard operation
bean copied to clipboard

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

Open bartsidee opened this issue 11 years ago • 7 comments

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');

bartsidee avatar Jan 28 '14 14:01 bartsidee

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))
          }
        }

bartsidee avatar Jan 28 '14 15:01 bartsidee

Duplicate of #80 Yes we should fix this.

ryanve avatar Jan 28 '14 17:01 ryanve

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

rvagg avatar Jan 28 '14 19:01 rvagg

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

ded avatar Jan 28 '14 19:01 ded

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

ryanve avatar Jan 28 '14 21:01 ryanve

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

ded avatar Jan 28 '14 21:01 ded

Sounds like a plan.

ryanve avatar Jan 28 '14 22:01 ryanve