Skip to content

Commit

Permalink
Fix to issue #99 without breaking any other tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
imjoshdean committed Nov 13, 2012
1 parent 907c589 commit 9377c5e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
19 changes: 17 additions & 2 deletions model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
})
}
});
Expand Down Expand Up @@ -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;
};
};
});
Expand Down
39 changes: 39 additions & 0 deletions model/model_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",{
Expand Down

0 comments on commit 9377c5e

Please sign in to comment.