Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/module/api/common-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', 'RMUtils', function

return this.$always(function() {

var defaultOptions = {};

if(action && action.aborter) {
defaultOptions.timeout = action.aborter.promise;
}

_options = angular.extend(defaultOptions, _options);

this.$response = null;
this.$status = 'pending';
this.$dispatch('before-request', [_options]);
Expand Down Expand Up @@ -431,7 +439,8 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', 'RMUtils', function
*/
$action: function(_fun) {
var status = {
canceled: false
canceled: false,
aborter: $q.defer()
}, pending = this.$pending || (this.$pending = []);

pending.push(status);
Expand Down Expand Up @@ -473,6 +482,24 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', 'RMUtils', function
return this;
},

/**
* @memberof CommonApi#
*
* @description Aborts all pending actions registered with $action.
*
* @return {CommonApi} self
*/
$abort: function() {
// abort every pending request.
if(this.$pending) {
angular.forEach(this.$pending, function(_status) {
_status.aborter.resolve();
});
}

return this;
},

/**
* @memberof CommonApi#
*
Expand All @@ -495,4 +522,4 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', 'RMUtils', function

return CommonApi;

}]);
}]);
6 changes: 4 additions & 2 deletions src/module/fastq.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Synchronous promise implementation (partial)
*
*/
RMModule.factory('RMFastQ', [function() {
RMModule.factory('RMFastQ', ['$q', function($q) {

var isFunction = angular.isFunction,
catchError = function(_error) {
Expand Down Expand Up @@ -78,6 +78,8 @@ RMModule.factory('RMFastQ', [function() {
return simpleQ(_val, false);
},

wrap: wrappedQ
wrap: wrappedQ,

defer: $q.defer
};
}]);
27 changes: 27 additions & 0 deletions test/common-api-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,33 @@ describe('Restmod model class:', function() {
});
});

describe('$abort', function() {
it('should abort every pending action', function() {
var bike = Bike.$new(),
spy = jasmine.createSpy('done');

$httpBackend.expect('GET', '/api/bikes/1').respond(200);

bike.$action(function() {
this.$send({ url: '/api/bikes/1', method: 'GET' }).$asPromise().catch(function(response) {
var response = response.$response;
expect(response.data).toBeUndefined();
expect(response.status).toBe(-1);
expect(response.config.url).toBe('/api/bikes/1');
spy();
});
});

$rootScope.$apply(function() {
bike.$abort();
});

expect(spy).toHaveBeenCalled();
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
});

describe('$action', function() {

it('should keep action in pending action list until its done', function() {
Expand Down