Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($resource): add support for cancelling requests
Introduced changes: - Deprecate passing a promise as `timeout` (for `$resource` actions). It never worked correctly anyway. Now a warning is logged (using `$log.debug()`) and the property is removed. - Provide a `cancelRequest` static method on the Resource that will abort the request (if it's not already completed or aborted). If there is a numeric `timeout` specified on the action's configuration, this method will have no effect. Example usage: ```js var Post = $resource('/posts/:id', {id: '@id'}, { get: { method: 'GET' } }); var currentPost = Post.get({id: 1}); ... // A moment later the user selects another post, so // we don't need the previous request any more Post.cancelRequest(currentPost); currentPost = Post.get({id: 2}); ... ``` BREAKING CHANGE: Using a promise as `timeout` is no longer supported and will log a warning. It never worked the way it was supposed to anyway. Before: ```js var deferred = $q.defer(); var User = $resource('/api/user/:id', {id: '@id'}, { get: {method: 'GET', timeout: deferred.promise} }); var user = User.get({id: 1}); // sends a request deferred.resolve(); // aborts the request // Now, we need to re-define `User` passing a new promise as `timeout` // or else all subsequent requests from `someAction` will be aborted User = $resource(...); user = User.get({id: 2}); ``` After: ```js var User = $resource('/api/user/:id', {id: '@id'}, { get: {method: 'GET'} }); var user = User.get({id: 1}); // sends a request User.cancelRequest(instance); // aborts the request user = User.get({id: 2}); ``` Fixes angular#9332 Closes angular#13050 Closes angular#13058
- Loading branch information