-
-
Save jaubourg/1387553 to your computer and use it in GitHub Desktop.
Singleton wrapper around $.Callbacks to emulate Backbone events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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