diff --git a/src/module/api/common-api.js b/src/module/api/common-api.js index 5ed7a44..2328640 100644 --- a/src/module/api/common-api.js +++ b/src/module/api/common-api.js @@ -366,42 +366,32 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q, this.$status = 'pending'; this.$dispatch('before-request', [_options]); - var dsp = this.$dispatcher(), _this = this; - return $http(_options).then(function(_response) { - - return _this.$decorate(dsp, function() { - if(action && action.canceled) { - // if request was canceled during request, ignore post request actions. - this.$status = 'canceled'; - } else { - this.$status = 'ok'; - this.$response = _response; - - this.$dispatch('after-request', [_response]); - if(_success) _success.call(this, _response); - } - }); - - }, function(_response) { - - return _this.$decorate(dsp, function() { - if(action && action.canceled) { - // if request was canceled during request, ignore error handling - this.$status = 'canceled'; - return this; - } else { - this.$status = 'error'; - this.$response = _response; - - // IDEA: Consider flushing pending request in case of an error. Also continue ignoring requests - // until the error flag is reset by user. - - this.$dispatch('after-request-error', [_response]); - if(_error) _error.call(this, _response); - return $q.reject(this); - } - }); - }); + return $http(_options).then(wrapPromise(this, function() { + if(action && action.canceled) { + // if request was canceled during request, ignore post request actions. + this.$status = 'canceled'; + } else { + this.$status = 'ok'; + this.$response = this.$last; + this.$dispatch('after-request', [this.$last]); + if(_success) _success.call(this, this.$last); + } + }), wrapPromise(this, function() { + if(action && action.canceled) { + // if request was canceled during request, ignore error handling + this.$status = 'canceled'; + } else { + this.$status = 'error'; + this.$response = this.$last; + + // IDEA: Consider flushing pending request in case of an error. Also continue ignoring requests + // until the error flag is reset by user. + + this.$dispatch('after-request-error', [this.$last]); + if(_error) _error.call(this, this.$last); + return $q.reject(this); // TODO: this will step over any promise generated in _error!! + } + })); }); }, diff --git a/test/collection-api-spec.js b/test/collection-api-spec.js index 48d2f6b..7c4ee57 100644 --- a/test/collection-api-spec.js +++ b/test/collection-api-spec.js @@ -2,7 +2,7 @@ describe('Restmod collection:', function() { - var restmod, $httpBackend, Bike, query; + var restmod, $httpBackend, $rootScope, Bike, query; beforeEach(module('restmod')); @@ -14,6 +14,7 @@ describe('Restmod collection:', function() { // mock api $httpBackend = $injector.get('$httpBackend'); + $rootScope = $injector.get('$rootScope'); $httpBackend.when('GET', '/api/bikes?brand=trek').respond([ { model: 'Slash' }, { model: 'Remedy' } ]); $httpBackend.when('GET', '/api/bikes?brand=giant').respond([ { model: 'Reign' } ]); })); @@ -40,6 +41,15 @@ describe('Restmod collection:', function() { expect(query.length).toEqual(3); }); + it('should call $then callbacks after fetch completes (Issue #154)', function() { + var queryLen; + query = query.$search().$then(function() { + queryLen = query.length; + }); + $httpBackend.flush(); + expect(queryLen).toEqual(2); + }); + }); describe('$clear', function() {