Skip to content

Instantly share code, notes, and snippets.

@yethee
Forked from mxriverlynn/1.js
Created February 7, 2012 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yethee/1761003 to your computer and use it in GitHub Desktop.
Save yethee/1761003 to your computer and use it in GitHub Desktop.
Backbone.Marionette.Callbaks
// Callbacks
// ---------
// A simple way of managing a collection of callbacks
// and executing them at a later point in time, using jQuery's
// `Callbacks` object.
Marionette.Callbacks = function(){
this.callbacks = $.Callbacks("once memory");
};
_.extend(Marionette.Callbacks.prototype, {
// Add a callback to be executed. Callbacks added here are
// guaranteed to execute, even if they are added after the
// `run` method is called.
add: function(callback){
this.callbacks.add(callback);
},
// Run all registered callbacks with the context specified.
// Additional callbacks can be added after this has been run
// and they will still be executed.
run: function(context, options){
this.callbacks.fireWith(context, [options]);
}
});
@mxriverlynn
Copy link

nice! this would eliminate the need for my Callbacks object entirely.

does this support the scenario where run is called before add is called?

@yethee
Copy link
Author

yethee commented Feb 8, 2012

If Callbacks object will be created with once and memory flags, then its behaviour is the same as the deferred.

I trying play with specs, they all passed when using jQuery's callbacks instead of the deferred object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment