Is there a way to achieve this kind of statement (e.g. an existing plugin to extend
.on
)?
I'm not aware of one. Would be nearly trivial to write. I'm not seeing much advantage to it, though, compared with chaining:
$this.on('click','.trigger a',rowTrigger)
.on('click','.actions nav a',rowAction);
But again, the plug-in isn't complicated. Here's an untested rough draft:
jQuery.fn.onAny = function(options) {
var eventName, eventSpec, eventEntry, selector, i;
for (eventName in options) {
eventSpec = options[eventName];
if (jQuery.isArray(eventSpec)) {
// Your example, where each event name has an array of objects
// keyed by selector, where the value is the handler
for (i = 0; i < eventSpec.length; ++i) {
eventEntry = eventSpec[i];
for (selector in eventEntry) {
this.on(eventName, selector, eventEntry[selector]);
}
}
}
else {
// Assuming just a straight handler here
this.on(eventName, eventSpec);
}
}
return this;
};