question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple events with the on() method

See original GitHub issue

Expected 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:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
bdoughertycommented, Aug 29, 2016

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:


function eventHandler(data) {
    doSomething();
}

player.on('play', eventHandler);
player.on('pause', eventHandler);

without too much trouble.

0reactions
luwescommented, Mar 2, 2018

This is something that will not get added for now. One simple solution would be to extend the VimeoPlayer class and override the on or off methods if you really need this behaviour.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found