Skip to content
This repository
Browse code

Fix to issue #99 without breaking any other tests.

  • Loading branch information...
commit 9377c5e7fe012318dbac370b748943ba9f3b4581 1 parent 907c589
Josh Dean authored November 13, 2012
19  model/model.js
@@ -18,6 +18,13 @@ steal('can/util','can/observe', function( can ) {
18 18
 		},function(){
19 19
 			d.rejectWith(this, arguments);
20 20
 		});
  21
+
  22
+		if(typeof def.abort === 'function') {
  23
+			d.abort = function() {
  24
+				return def.abort();
  25
+			}
  26
+		}
  27
+
21 28
 		return d;
22 29
 	},
23 30
 		modelNum = 0,
@@ -579,7 +586,12 @@ steal('can/util','can/observe', function( can ) {
579 586
 					can.Construct._overwrite(self, base, name,function(){
580 587
 						// increment the numer of requests
581 588
 						this._reqs++;
582  
-						return newMethod.apply(this, arguments).then(clean, clean);
  589
+						var def = newMethod.apply(this, arguments);
  590
+						var then = def.then(clean, clean);
  591
+						then.abort = def.abort;
  592
+
  593
+						// attach abort to our then and return it
  594
+						return then;
583 595
 					})
584 596
 				}
585 597
 			});
@@ -1046,7 +1058,10 @@ steal('can/util','can/observe', function( can ) {
1046 1058
 	}, function( method, name ) {
1047 1059
 		can.Model[name] = function( oldFind ) {
1048 1060
 			return function( params, success, error ) {
1049  
-				return pipe( oldFind.call( this, params ), this, method ).then( success, error );
  1061
+				var def = pipe( oldFind.call( this, params ), this, method );
  1062
+				def.then( success, error );
  1063
+				// return the original promise
  1064
+				return def;
1050 1065
 			};
1051 1066
 		};
1052 1067
 	});
39  model/model_test.js
@@ -114,6 +114,45 @@ asyncTest("findAll deferred reject", function() {
114 114
     }, 200);
115 115
 });
116 116
 
  117
+if(window.jQuery) {
  118
+
  119
+asyncTest("findAll abort", function() {
  120
+	expect(4);
  121
+	var df;
  122
+	can.Model("Person", {
  123
+		findAll : function(params, success, error) {
  124
+			df = can.Deferred();
  125
+			df.then(function() {
  126
+				ok(!params.abort,'not aborted');
  127
+			},function() {
  128
+				ok(params.abort,'aborted');
  129
+			});
  130
+			return df.promise({
  131
+				abort: function() {
  132
+					df.reject();
  133
+				}
  134
+			});
  135
+		}
  136
+	},{});
  137
+	var resolvePromise = Person.findAll({ abort : false}).done(function() {
  138
+		ok(true,'resolved');
  139
+	});
  140
+	var resolveDf = df;
  141
+	var abortPromise = Person.findAll({ abort : true}).fail(function() {
  142
+		ok(true,'failed');
  143
+	});
  144
+
  145
+	setTimeout(function() {
  146
+		resolveDf.resolve();
  147
+        abortPromise.abort();
  148
+
  149
+        // continue the test
  150
+        start();
  151
+    }, 200);
  152
+});
  153
+
  154
+}
  155
+
117 156
 test("findOne deferred", function(){
118 157
 	if(window.jQuery){
119 158
 		can.Model("Person",{

0 notes on commit 9377c5e

Please sign in to comment.
Something went wrong with that request. Please try again.