Skip to content

Commit d5542f0

Browse files
committed
fix(common): fixes $send not properly handling the $promise property.
Closes #154
1 parent 61aa235 commit d5542f0

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

src/module/api/common-api.js

+26-36
Original file line numberDiff line numberDiff line change
@@ -366,42 +366,32 @@ RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', function($http, $q,
366366
this.$status = 'pending';
367367
this.$dispatch('before-request', [_options]);
368368

369-
var dsp = this.$dispatcher(), _this = this;
370-
return $http(_options).then(function(_response) {
371-
372-
return _this.$decorate(dsp, function() {
373-
if(action && action.canceled) {
374-
// if request was canceled during request, ignore post request actions.
375-
this.$status = 'canceled';
376-
} else {
377-
this.$status = 'ok';
378-
this.$response = _response;
379-
380-
this.$dispatch('after-request', [_response]);
381-
if(_success) _success.call(this, _response);
382-
}
383-
});
384-
385-
}, function(_response) {
386-
387-
return _this.$decorate(dsp, function() {
388-
if(action && action.canceled) {
389-
// if request was canceled during request, ignore error handling
390-
this.$status = 'canceled';
391-
return this;
392-
} else {
393-
this.$status = 'error';
394-
this.$response = _response;
395-
396-
// IDEA: Consider flushing pending request in case of an error. Also continue ignoring requests
397-
// until the error flag is reset by user.
398-
399-
this.$dispatch('after-request-error', [_response]);
400-
if(_error) _error.call(this, _response);
401-
return $q.reject(this);
402-
}
403-
});
404-
});
369+
return $http(_options).then(wrapPromise(this, function() {
370+
if(action && action.canceled) {
371+
// if request was canceled during request, ignore post request actions.
372+
this.$status = 'canceled';
373+
} else {
374+
this.$status = 'ok';
375+
this.$response = this.$last;
376+
this.$dispatch('after-request', [this.$last]);
377+
if(_success) _success.call(this, this.$last);
378+
}
379+
}), wrapPromise(this, function() {
380+
if(action && action.canceled) {
381+
// if request was canceled during request, ignore error handling
382+
this.$status = 'canceled';
383+
} else {
384+
this.$status = 'error';
385+
this.$response = this.$last;
386+
387+
// IDEA: Consider flushing pending request in case of an error. Also continue ignoring requests
388+
// until the error flag is reset by user.
389+
390+
this.$dispatch('after-request-error', [this.$last]);
391+
if(_error) _error.call(this, this.$last);
392+
return $q.reject(this); // TODO: this will step over any promise generated in _error!!
393+
}
394+
}));
405395
});
406396
},
407397

test/collection-api-spec.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
var restmod, $httpBackend, Bike, query;
5+
var restmod, $httpBackend, $rootScope, Bike, query;
66

77
beforeEach(module('restmod'));
88

@@ -14,6 +14,7 @@ describe('Restmod collection:', function() {
1414

1515
// mock api
1616
$httpBackend = $injector.get('$httpBackend');
17+
$rootScope = $injector.get('$rootScope');
1718
$httpBackend.when('GET', '/api/bikes?brand=trek').respond([ { model: 'Slash' }, { model: 'Remedy' } ]);
1819
$httpBackend.when('GET', '/api/bikes?brand=giant').respond([ { model: 'Reign' } ]);
1920
}));
@@ -40,6 +41,15 @@ describe('Restmod collection:', function() {
4041
expect(query.length).toEqual(3);
4142
});
4243

44+
it('should call $then callbacks after fetch completes (Issue #154)', function() {
45+
var queryLen;
46+
query = query.$search().$then(function() {
47+
queryLen = query.length;
48+
});
49+
$httpBackend.flush();
50+
expect(queryLen).toEqual(2);
51+
});
52+
4353
});
4454

4555
describe('$clear', function() {

0 commit comments

Comments
 (0)