Skip to content

Commit

Permalink
Remove hard dependency on jQuery.Deferred quirks
Browse files Browse the repository at this point in the history
jQuery.Deferred objects are not compliant with the Promises/A+ standard.
Promise#done() is not part of the standard and has different meanings in
different promise libraries.

This fixes the Deffered dependency discussed in marionettejs#980. This also exposes
Marionette.Deferred as a means to configure the desired implementation.

e.g.
```
Backbone.Marionette.Deferred = Q.defer; // kriskowal/q
Backbone.Marionette.Deferred = when.defer; // cujojs/when
```
  • Loading branch information
cmaher committed Jun 1, 2014
1 parent 324141d commit 2a22ad6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
29 changes: 29 additions & 0 deletions spec/javascripts/callbacks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,33 @@ describe("callbacks", function(){
});
});

describe("when Marionette.Deferred().promise is an object", function(){
var spy, sandbox;

beforeEach(function(){
sandbox = sinon.sandbox.create();

sandbox.stub(Backbone.Marionette, "Deferred", function(){
var deferred = Backbone.Marionette.$.Deferred();
deferred.promise = deferred.promise();
return deferred;
});

var callbacks = new Backbone.Marionette.Callbacks();

spy = sinon.spy();
callbacks.add(spy);

callbacks.run();
});

afterEach(function(){
sandbox.restore();
});

it("should execute the callbacks", function(){
expect(spy).toHaveBeenCalled();
});
});

});
3 changes: 3 additions & 0 deletions src/build/marionette.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ var Marionette = (function(global, Backbone, _){
// Get the DOM manipulator for later use
Marionette.$ = Backbone.$;

// Get the Deferred creator for later use
Marionette.Deferred = Marionette.$.Deferred;

// @include ../marionette.helpers.js
// @include ../marionette.triggermethod.js
// @include ../marionette.domRefresh.js
Expand Down
3 changes: 3 additions & 0 deletions src/build/marionette.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var Marionette = (function(global, Backbone, _){
// Get the DOM manipulator for later use
Marionette.$ = Backbone.$;

// Get the Deferred creator for later use
Marionette.Deferred = Marionette.$.Deferred;

// @include ../marionette.helpers.js
// @include ../marionette.triggermethod.js
// @include ../marionette.domRefresh.js
Expand Down
19 changes: 13 additions & 6 deletions src/marionette.callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// and executing them at a later point in time, using jQuery's
// `Deferred` object.
Marionette.Callbacks = function(){
this._deferred = Marionette.$.Deferred();
this._deferred = Marionette.Deferred();
this._callbacks = [];
};

Expand All @@ -15,26 +15,33 @@ _.extend(Marionette.Callbacks.prototype, {
// guaranteed to execute, even if they are added after the
// `run` method is called.
add: function(callback, contextOverride){
var promise = _.result(this._deferred, 'promise');

this._callbacks.push({cb: callback, ctx: contextOverride});

this._deferred.done(function(context, options){
if (contextOverride){ context = contextOverride; }
callback.call(context, options);
promise.then(function(args){
if (contextOverride){ args.context = contextOverride; }
callback.call(args.context, args.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);
var args = {
options: options,
context: context
};

this._deferred.resolve(args);
},

// 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 callbacks = this._callbacks;
this._deferred = Marionette.$.Deferred();
this._deferred = Marionette.Deferred();
this._callbacks = [];

_.each(callbacks, function(cb){
Expand Down

0 comments on commit 2a22ad6

Please sign in to comment.