Skip to content

Commit

Permalink
Added an optional 'scope' parameter to Deferred/Promise callback regi…
Browse files Browse the repository at this point in the history
…stration.

Modified `Deft.promise.Deferred::then()`, `Deft.promise.Deferred::otherwise()`, and `Deft.promise.Deferred::always()` to accept a new parameter (or `scope` parameter within a configuration object) that defines the scope in which the specified callbacks should be executed.
Modified `Deft.promise.Promise::then()`, `Deft.promise.Promise::otherwise()`, and `Deft.promise.Promise::always()` to pass through all parameters to the corresponding methods in the backing `Deft.promise.Deferred`.

Fixes #33
  • Loading branch information
John Yanarella committed Aug 1, 2012
1 parent a3289bb commit 01631e2
Show file tree
Hide file tree
Showing 8 changed files with 980 additions and 410 deletions.
36 changes: 23 additions & 13 deletions build/deft-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -1201,11 +1201,11 @@ Ext.define('Deft.promise.Deferred', {
*/

then: function(callbacks) {
var callback, cancelCallback, deferred, failureCallback, progressCallback, successCallback, wrapCallback, wrapProgressCallback, _i, _len, _ref;
var callback, cancelCallback, deferred, failureCallback, progressCallback, scope, successCallback, wrapCallback, wrapProgressCallback, _i, _len, _ref;
if (Ext.isObject(callbacks)) {
successCallback = callbacks.success, failureCallback = callbacks.failure, progressCallback = callbacks.progress, cancelCallback = callbacks.cancel;
successCallback = callbacks.success, failureCallback = callbacks.failure, progressCallback = callbacks.progress, cancelCallback = callbacks.cancel, scope = callbacks.scope;
} else {
successCallback = arguments[0], failureCallback = arguments[1], progressCallback = arguments[2], cancelCallback = arguments[3];
successCallback = arguments[0], failureCallback = arguments[1], progressCallback = arguments[2], cancelCallback = arguments[3], scope = arguments[4];
}
_ref = [successCallback, failureCallback, progressCallback, cancelCallback];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
Expand All @@ -1222,7 +1222,7 @@ Ext.define('Deft.promise.Deferred', {
var result;
if (Ext.isFunction(callback)) {
try {
result = callback(value);
result = callback.call(scope, value);
if (result instanceof Ext.ClassManager.get('Deft.promise.Promise') || result instanceof Ext.ClassManager.get('Deft.promise.Deferred')) {
result.then(Ext.bind(deferred.resolve, deferred), Ext.bind(deferred.reject, deferred), Ext.bind(deferred.update, deferred), Ext.bind(deferred.cancel, deferred));
} else {
Expand All @@ -1243,7 +1243,7 @@ Ext.define('Deft.promise.Deferred', {
return function(value) {
var result;
if (Ext.isFunction(callback)) {
result = callback(value);
result = callback.call(scope, value);
deferred.update(result);
} else {
deferred.update(value);
Expand All @@ -1257,20 +1257,30 @@ Ext.define('Deft.promise.Deferred', {
Returns a new {@link Deft.promise.Promise} with the specified callback registered to be called when this {@link Deft.promise.Deferred} is rejected.
*/

otherwise: function(callback) {
otherwise: function(callback, scope) {
var _ref;
if (Ext.isObject(callback)) {
_ref = callback, callback = _ref.fn, scope = _ref.scope;
}
return this.then({
failure: callback
failure: callback,
scope: scope
});
},
/**
Returns a new {@link Deft.promise.Promise} with the specified callback registered to be called when this {@link Deft.promise.Deferred} is either resolved, rejected, or cancelled.
*/

always: function(callback) {
always: function(callback, scope) {
var _ref;
if (Ext.isObject(callback)) {
_ref = callback, callback = _ref.fn, scope = _ref.scope;
}
return this.then({
success: callback,
failure: callback,
cancel: callback
cancel: callback,
scope: scope
});
},
/**
Expand Down Expand Up @@ -1637,15 +1647,15 @@ Ext.define('Deft.promise.Promise', {
Returns a new {@link Deft.promise.Promise} with the specified callback registered to be called when this {@link Deft.promise.Promise} is rejected.
*/

otherwise: function(callback) {
return this.deferred.otherwise(callback);
otherwise: function(callback, scope) {
return this.deferred.otherwise.apply(this.deferred, arguments);
},
/**
Returns a new {@link Deft.promise.Promise} with the specified callback registered to be called when this {@link Deft.promise.Promise} is resolved, rejected or cancelled.
*/

always: function(callback) {
return this.deferred.always(callback);
always: function(callback, scope) {
return this.deferred.always.apply(this.deferred, arguments);
},
/**
Cancel this {@link Deft.promise.Promise} and notify relevant callbacks.
Expand Down
2 changes: 1 addition & 1 deletion build/deft.js

Large diffs are not rendered by default.

Loading

0 comments on commit 01631e2

Please sign in to comment.