Skip to content Skip to sidebar Skip to footer

Using "class/object" Mootools-style Events In Jquery

One of the nice things about MooTools, is that it lets you easily assign/fire events to objects, for example: var playerSingleton = new (new Class({ Implements: [Events], init

Solution 1:

jQuery's bind and trigger seem to work on normal objects. Haven't seen the source code to see how it works (if it's part of the public API or not), but it does. See this discussion from last year poking around the same idea.

player is a regular object, with methods to set volume, and add listeners for volume change. an example here.

var player = {
    setVolume: function() {
        $(this).trigger("volumeChanged");
    },

    addVolumeChangeHandler: function(fn) {
        $(this).bind("volumeChanged", fn);
    }
};

// add a listener
player.addVolumeChangeHandler(function() {
    alert("volume has been changed");
});

// change volume (should fire the attached listener)
player.setVolume(); // alerts "volume has been changed"

Post a Comment for "Using "class/object" Mootools-style Events In Jquery"