Skip to content

Instantly share code, notes, and snippets.

@jaubourg
Forked from DaveStein/Events.js
Created November 23, 2011 00:37
Show Gist options
  • Save jaubourg/1387553 to your computer and use it in GitHub Desktop.
Save jaubourg/1387553 to your computer and use it in GitHub Desktop.
Singleton wrapper around $.Callbacks to emulate Backbone events
var Event = (function( $ ) {
var cache = {},
slice = [].slice,
fake = {
remove: $.noop,
fireWith: $.noop
};
function get( ev, createIfNeeded ) {
return cache[ ev ] || createIfNeeded ? ( cache[ ev ] = $.Callbacks( "stopOnFalse" ) ) : fake;
}
return {
bind : function( ev, callback, context ) {
get( ev, true ).add( $.proxy( callback, context ) );
},
unbind : function( ev, callback ) {
get( ev ).remove( callback );
},
trigger : function( ev ) {
get( ev ).fireWith( null, slice.call( arguments, 1 ) );
}
};
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment