Plato on Github
Report Home
callbacks.js
Maintainability
79.98
Estimated # Bugs
0.23
Difficulty
12.50
SLOC/LSLOC
38 / 21
Function weight
By Complexity
By SLOC
// Callbacks // --------- // A simple way of managing a collection of callbacks // and executing them at a later point in time, using jQuery's // `Deferred` object. Marionette.Callbacks = function(){ this._deferred = $.Deferred(); this._callbacks = []; }; _.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, contextOverride){ this._callbacks.push({cb: callback, ctx: contextOverride}); this._deferred.done(function(context, options){ if (contextOverride){ context = contextOverride; } callback.call(context, options); }); }, // 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(options, context){ this._deferred.resolve(context, options); }, // Resets the list of callbacks to be run, allowing the same list // to be run multiple times - whenever the `run` method is called. reset: function(){ var that = this; var callbacks = this._callbacks; this._deferred = $.Deferred(); this._callbacks = []; _.each(callbacks, function(cb){ that.add(cb.cb, cb.ctx); }); } });