@@ -202,6 +202,8 @@ function shallowClearAndCopy(src, dst) {
202
202
* rejection), `false` before that. Knowing if the Resource has been resolved is useful in
203
203
* data-binding.
204
204
*
205
+ * - `$abort`: a method to abort the request if it has not completed yet.
206
+ *
205
207
* @example
206
208
*
207
209
* # Credit card resource
@@ -303,7 +305,7 @@ function shallowClearAndCopy(src, dst) {
303
305
* </pre>
304
306
*/
305
307
angular . module ( 'ngResource' , [ 'ng' ] ) .
306
- factory ( '$resource' , [ '$http' , '$q' , function ( $http , $q ) {
308
+ factory ( '$resource' , [ '$http' , '$q' , '$timeout' , function ( $http , $q , $timeout ) {
307
309
308
310
var DEFAULT_ACTIONS = {
309
311
'get' : { method :'GET' } ,
@@ -501,6 +503,16 @@ angular.module('ngResource', ['ng']).
501
503
}
502
504
} ) ;
503
505
506
+ // If parameters do not contain `timeout` which is a promise, create it,
507
+ // so that later this call can be aborted by resolving this promise
508
+ var timeout = httpConfig . timeout ;
509
+ if ( ! timeout || ! timeout . then ) {
510
+ var timeoutDeferred = $q . defer ( ) ;
511
+ httpConfig . timeout = timeoutDeferred . promise ;
512
+ // If timeout is specified in milliseconds, use it to abort via promise
513
+ if ( timeout ) $timeout ( timeoutDeferred . resolve , timeout ) ;
514
+ }
515
+
504
516
if ( hasBody ) httpConfig . data = data ;
505
517
route . setUrlParams ( httpConfig ,
506
518
extend ( { } , extractParams ( data , action . params || { } ) , params ) ,
@@ -557,6 +569,7 @@ angular.module('ngResource', ['ng']).
557
569
// - return the instance / collection
558
570
value . $promise = promise ;
559
571
value . $resolved = false ;
572
+ if ( timeoutDeferred ) value . $abort = timeoutDeferred . resolve ;
560
573
561
574
return value ;
562
575
}
0 commit comments