Multiple events with the on() method
See original GitHub issueExpected Behavior
Being able to send multiple events to the on() function to reduce copy pasted code.
Actual Behavior
Unable to do it as the function apparently only supports one event as argument.
Steps to Reproduce
Asuming jQuery & player.js:
var player = new Vimeo.Player($('#video'));
// Works
player.on('play', function() {
// doSomething
});
player.on('pause', function() {
// doSomething
});
// What I'd like to have
// like jQuery
player.on('play pause', function() {
// doSomething
});
// as an array
player.on(['play', 'pause'], function() {
// doSomething
});
My naive approach to adding this functionality (with an array) would be to modify the method as follows:
/**
* Add an event listener for the specified event(s). Will call the
* callback with a single parameter, `data`, that contains the data for
* that/those event(s).
*
* @author Brad Dougherty <brad@vimeo.com>
* @param {array} eventsName An array containing the name of the event(s).
* @param {function(*)} callback The function to call when the event(s) fire(s).
* @return {void}
*/
on(eventsName, callback) {
if (!eventsName) {
throw new TypeError('You must pass an event name.');
}
if (!callback) {
throw new TypeError('You must pass a callback function.');
}
if (typeof callback !== 'function') {
throw new TypeError('The callback must be a function.');
}
for (var i; i < eventsName.length; i++) {
const callbacks = getCallbacks(this, `event:${eventsName[i]}`);
if (callbacks.length === 0) {
this.callMethod('addEventListener', eventsName[i]).catch(() => {
// Ignore the error. There will be an error event fired that
// will trigger the error callback if they are listening.
});
}
storeCallback(this, `event:${eventsName[i]}`, callback);
}
}
Disclaimer: I didn’t test this nor check how the other functions called by this one work.
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How can I trigger the same function from multiple events with ...
You can use .on() to bind a function to multiple events: $('#element').on('keyup keypress blur change', function(e) { // e.type is the type ...
Read more >jQuery on() Method - W3Schools
The on() method attaches one or more event handlers for the selected elements and child elements. As of jQuery version 1.7, the on()...
Read more >Handling Events - jQuery Learning Center
on() method provides several useful features: Bind any event triggered on the selected elements to an event handler; Bind multiple events to one ......
Read more >Multiple ways of using jQuery On Method - YogiHosting
The jQuery .on() can attach multiple events on an element. In the below code I have attached 2 events to the p element....
Read more >Javascript: adding a single function to multiple events
Javascript: adding a single function to multiple events. Hi everybody, currently I have something like this: Copy Code window.addEventListener('load' ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Are there any other events besides play/pause that you’d want to have a common listener for? I’m open to a PR that adds the array syntax, but I’m not sure how useful it is overall when you could do something like:
without too much trouble.
This is something that will not get added for now. One simple solution would be to extend the VimeoPlayer class and override the
onoroffmethods if you really need this behaviour.