Using "class/object" MooTools-style events in jQuery

Posted by Infinity on Stack Overflow See other posts from Stack Overflow or by Infinity
Published on 2010-04-12T20:17:52Z Indexed on 2010/04/12 20:43 UTC
Read the original article Hit count: 381

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],

  initialize: function() {},

  setVolume: function() { 
    // do some stuff..
    this.fireEvent('volumeChanged')
  }

}));

// Somewhere else...

playerSingleton.addEvent('volumeChanged', function() {
  // do something when volume changes
});

playerSingleton.setVolume(75);
// bam our event fires.

How would something like this be done with jQuery? I know there's .bind and .trigger, but it seems like the only way to do this is to bind/fire events to the window object:

$(window).bind('volumeChanged', fn);

Is there anything better than this, more like the MooTools approach? Thanks!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about mootools