@@ -65,6 +65,7 @@ function shallowClearAndCopy(src, dst) {
65
65
* @requires $http
66
66
* @requires ng.$log
67
67
* @requires $q
68
+ * @requires $timeout
68
69
*
69
70
* @description
70
71
* A factory which creates a resource object that lets you interact with
@@ -160,7 +161,6 @@ function shallowClearAndCopy(src, dst) {
160
161
* will be cancelled (if not already completed) by calling `$cancelRequest()` on the call's
161
162
* return value. Calling `$cancelRequest()` for a non-cancellable or an already
162
163
* completed/cancelled request will have no effect.<br />
163
- * **Note:** If a timeout is specified in millisecondes, `cancellable` is ignored.
164
164
* - **`withCredentials`** - `{boolean}` - whether to set the `withCredentials` flag on the
165
165
* XHR object. See
166
166
* [requests with credentials](https://developer.mozilla.org/en/http_access_control#section_5)
@@ -416,7 +416,7 @@ angular.module('ngResource', ['ng']).
416
416
}
417
417
} ;
418
418
419
- this . $get = [ '$http' , '$log' , '$q' , function ( $http , $log , $q ) {
419
+ this . $get = [ '$http' , '$log' , '$q' , '$timeout' , function ( $http , $log , $q , $timeout ) {
420
420
421
421
var noop = angular . noop ,
422
422
forEach = angular . forEach ,
@@ -575,20 +575,19 @@ angular.module('ngResource', ['ng']).
575
575
576
576
forEach ( actions , function ( action , name ) {
577
577
var hasBody = / ^ ( P O S T | P U T | P A T C H ) $ / i. test ( action . method ) ;
578
- var cancellable = false ;
579
-
580
- if ( ! angular . isNumber ( action . timeout ) ) {
581
- if ( action . timeout ) {
582
- $log . debug ( 'ngResource:\n' +
583
- ' Only numeric values are allowed as `timeout`.\n' +
584
- ' Promises are not supported in $resource, because the same value would ' +
585
- 'be used for multiple requests. If you are looking for a way to cancel ' +
586
- 'requests, you should use the `cancellable` option.' ) ;
587
- delete action . timeout ;
588
- }
589
- cancellable = angular . isDefined ( action . cancellable ) ? action . cancellable :
590
- ( options && angular . isDefined ( options . cancellable ) ) ? options . cancellable :
591
- provider . defaults . cancellable ;
578
+ var numericTimeout = action . timeout ;
579
+ var cancellable = angular . isDefined ( action . cancellable ) ? action . cancellable :
580
+ ( options && angular . isDefined ( options . cancellable ) ) ? options . cancellable :
581
+ provider . defaults . cancellable ;
582
+
583
+ if ( numericTimeout && ! angular . isNumber ( numericTimeout ) ) {
584
+ $log . debug ( 'ngResource:\n' +
585
+ ' Only numeric values are allowed as `timeout`.\n' +
586
+ ' Promises are not supported in $resource, because the same value would ' +
587
+ 'be used for multiple requests. If you are looking for a way to cancel ' +
588
+ 'requests, you should use the `cancellable` option.' ) ;
589
+ delete action . timeout ;
590
+ numericTimeout = null ;
592
591
}
593
592
594
593
Resource [ name ] = function ( a1 , a2 , a3 , a4 ) {
@@ -639,6 +638,7 @@ angular.module('ngResource', ['ng']).
639
638
var responseErrorInterceptor = action . interceptor && action . interceptor . responseError ||
640
639
undefined ;
641
640
var timeoutDeferred ;
641
+ var numericTimeoutPromise ;
642
642
643
643
forEach ( action , function ( value , key ) {
644
644
switch ( key ) {
@@ -656,6 +656,10 @@ angular.module('ngResource', ['ng']).
656
656
if ( ! isInstanceCall && cancellable ) {
657
657
timeoutDeferred = $q . defer ( ) ;
658
658
httpConfig . timeout = timeoutDeferred . promise ;
659
+
660
+ if ( numericTimeout ) {
661
+ numericTimeoutPromise = $timeout ( timeoutDeferred . resolve , numericTimeout ) ;
662
+ }
659
663
}
660
664
661
665
if ( hasBody ) httpConfig . data = data ;
@@ -706,7 +710,8 @@ angular.module('ngResource', ['ng']).
706
710
value . $resolved = true ;
707
711
if ( ! isInstanceCall && cancellable ) {
708
712
value . $cancelRequest = angular . noop ;
709
- timeoutDeferred = httpConfig . timeout = null ;
713
+ $timeout . cancel ( numericTimeoutPromise ) ;
714
+ timeoutDeferred = numericTimeoutPromise = httpConfig . timeout = null ;
710
715
}
711
716
} ) ;
712
717
0 commit comments