Skip to content

Instantly share code, notes, and snippets.

@adelura

adelura/app.js Secret

Last active August 29, 2015 14:14
Show Gist options
  • Save adelura/d72870b378fcf64e91fe to your computer and use it in GitHub Desktop.
Save adelura/d72870b378fcf64e91fe to your computer and use it in GitHub Desktop.
function Button() {
this.open = function() {
// Developer decided that this event is semantically okay to be propagated.
this.fire( 'open', { propagate: true } );
};
}
Button.events = { open: 'open' };
// This constructor implement a collection interface.
function Toolbar() {}
Toolbar.events = { open: 'open' };
var toolbar = new Toolbar();
// Here is a default - straightforward behaviour with on WTF.
// Fourth argument is not provided which means that only `open` events fired on toolbar will call a callback.
toolbar.on( Toolbar.events.open, function( e ) {
console.log( e.target instanceof Toolbar ); // Will always be `true`.
} );
// Here we introduce a new, fourth parameter which filter out targets.
toolbar.on( Button.events.open, function( e ) {
console.log( e.target instanceof Button ); // Will always be `true`.
console.log( 'Hell, yeach button opened!' );
}, toolbar, 1337, /* Filtered by target - */Button );
toolbar.add( new Button() );
toolbar.add( new Button() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment