Permalink
Browse files

Fix to issue #99 without breaking any other tests.

  • Loading branch information...
1 parent 907c589 commit 9377c5e7fe012318dbac370b748943ba9f3b4581 @imjoshdean imjoshdean committed Nov 13, 2012
Showing with 56 additions and 2 deletions.
  1. +17 −2 model/model.js
  2. +39 −0 model/model_test.js
View
19 model/model.js
@@ -18,6 +18,13 @@ steal('can/util','can/observe', function( can ) {
},function(){
d.rejectWith(this, arguments);
});
+
+ if(typeof def.abort === 'function') {
+ d.abort = function() {
+ return def.abort();
+ }
+ }
+
return d;
},
modelNum = 0,
@@ -579,7 +586,12 @@ steal('can/util','can/observe', function( can ) {
can.Construct._overwrite(self, base, name,function(){
// increment the numer of requests
this._reqs++;
- return newMethod.apply(this, arguments).then(clean, clean);
+ var def = newMethod.apply(this, arguments);
+ var then = def.then(clean, clean);
+ then.abort = def.abort;
+
+ // attach abort to our then and return it
+ return then;
})
}
});
@@ -1046,7 +1058,10 @@ steal('can/util','can/observe', function( can ) {
}, function( method, name ) {
can.Model[name] = function( oldFind ) {
return function( params, success, error ) {
- return pipe( oldFind.call( this, params ), this, method ).then( success, error );
+ var def = pipe( oldFind.call( this, params ), this, method );
+ def.then( success, error );
+ // return the original promise
+ return def;
};
};
});
View
39 model/model_test.js
@@ -114,6 +114,45 @@ asyncTest("findAll deferred reject", function() {
}, 200);
});
+if(window.jQuery) {
+
+asyncTest("findAll abort", function() {
+ expect(4);
+ var df;
+ can.Model("Person", {
+ findAll : function(params, success, error) {
+ df = can.Deferred();
+ df.then(function() {
+ ok(!params.abort,'not aborted');
+ },function() {
+ ok(params.abort,'aborted');
+ });
+ return df.promise({
+ abort: function() {
+ df.reject();
+ }
+ });
+ }
+ },{});
+ var resolvePromise = Person.findAll({ abort : false}).done(function() {
+ ok(true,'resolved');
+ });
+ var resolveDf = df;
+ var abortPromise = Person.findAll({ abort : true}).fail(function() {
+ ok(true,'failed');
+ });
+
+ setTimeout(function() {
+ resolveDf.resolve();
+ abortPromise.abort();
+
+ // continue the test
+ start();
+ }, 200);
+});
+
+}
+
test("findOne deferred", function(){
if(window.jQuery){
can.Model("Person",{

0 comments on commit 9377c5e

Please sign in to comment.