Skip to content

Commit

Permalink
fix(common): fixes $send not properly handling the $promise property.
Browse files Browse the repository at this point in the history
Closes #154
  • Loading branch information
iobaixas committed Sep 25, 2014
1 parent 61aa235 commit d5542f0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
62 changes: 26 additions & 36 deletions src/module/api/common-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!!
}
}));
});
},

Expand Down
12 changes: 11 additions & 1 deletion test/collection-api-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe('Restmod collection:', function() {

var restmod, $httpBackend, Bike, query;
var restmod, $httpBackend, $rootScope, Bike, query;

beforeEach(module('restmod'));

Expand All @@ -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' } ]);
}));
Expand All @@ -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() {
Expand Down

0 comments on commit d5542f0

Please sign in to comment.