bean
bean copied to clipboard
The event argument is not passed for listners attached to a JS object
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');
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))
}
}
Duplicate of #80 Yes we should fix this.
@ryanve let's just get this done, want to do up a PR to make the needed changes?
interesting that this only comes up now. what would one do with an event object from a custom event?
@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.
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
Sounds like a plan.