Skip to content

Commit 9b56919

Browse files
author
Alexey Kuznetsov
committed
Added invokeApply parameter to $http to skip apply (pr angular#12557)
1 parent 45879a8 commit 9b56919

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"url": "https://github.com/angular/angular.js.git"
1010
},
1111
"engines": {
12-
"node": "^8.9.1",
12+
"node": ">8.9.1",
1313
"yarn": ">=1.3.2",
1414
"grunt": "^1.2.0"
1515
},

src/ng/http.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ function $HttpProvider() {
10311031
</file>
10321032
</example>
10331033
*/
1034-
function $http(requestConfig) {
1034+
function $http(requestConfig, invokeApply) {
10351035

10361036
if (!isObject(requestConfig)) {
10371037
throw minErr('$http')('badreq', 'Http request configuration must be an object. Received: {0}', requestConfig);
@@ -1155,7 +1155,7 @@ function $HttpProvider() {
11551155
}
11561156

11571157
// send request
1158-
return sendReq(config, reqData).then(transformResponse, transformResponse);
1158+
return sendReq(config, reqData, invokeApply).then(transformResponse, transformResponse);
11591159
}
11601160

11611161
function transformResponse(response) {
@@ -1318,7 +1318,7 @@ function $HttpProvider() {
13181318

13191319
function createShortMethods(names) {
13201320
forEach(arguments, function(name) {
1321-
$http[name] = function(url, config) {
1321+
$http[name] = function(url, config, invokeApply) {
13221322
return $http(extend({}, config || {}, {
13231323
method: name,
13241324
url: url
@@ -1330,7 +1330,7 @@ function $HttpProvider() {
13301330

13311331
function createShortMethodsWithData(name) {
13321332
forEach(arguments, function(name) {
1333-
$http[name] = function(url, data, config) {
1333+
$http[name] = function(url, data, config, invokeApply) {
13341334
return $http(extend({}, config || {}, {
13351335
method: name,
13361336
url: url,
@@ -1347,7 +1347,7 @@ function $HttpProvider() {
13471347
* !!! ACCESSES CLOSURE VARS:
13481348
* $httpBackend, defaults, $log, $rootScope, defaultCache, $http.pendingRequests
13491349
*/
1350-
function sendReq(config, reqData) {
1350+
function sendReq(config, reqData, invokeApply) {
13511351
var deferred = $q.defer(),
13521352
promise = deferred.promise,
13531353
cache,
@@ -1465,11 +1465,16 @@ function $HttpProvider() {
14651465
resolvePromise(response, status, headersString, statusText, xhrStatus);
14661466
}
14671467

1468-
if (useApplyAsync) {
1469-
$rootScope.$applyAsync(resolveHttpPromise);
1468+
var skipApply = (isDefined(invokeApply) && !invokeApply);
1469+
if (!skipApply) {
1470+
if (useApplyAsync) {
1471+
$rootScope.$applyAsync(resolveHttpPromise);
1472+
} else {
1473+
resolveHttpPromise();
1474+
if (!$rootScope.$$phase) $rootScope.$apply();
1475+
}
14701476
} else {
14711477
resolveHttpPromise();
1472-
if (!$rootScope.$$phase) $rootScope.$apply();
14731478
}
14741479
}
14751480

test/ng/httpSpec.js

+39
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,7 @@ describe('$http', function() {
10171017

10181018
describe('callbacks', function() {
10191019

1020+
10201021
it('should $apply after success callback', function() {
10211022
$httpBackend.when('GET').respond(200);
10221023
$http({method: 'GET', url: '/some'});
@@ -1071,6 +1072,44 @@ describe('$http', function() {
10711072
expect(uploadProgressFn).toHaveBeenCalledOnceWith(eventObj);
10721073
expect($rootScope.$digest).toHaveBeenCalledTimes(2);
10731074
});
1075+
1076+
it('should resolve promises anyway', function() {
1077+
$httpBackend.when('GET').respond(200);
1078+
var promiseCompleted = false;
1079+
$http({method: 'GET', url: '/some'}, false).then(function() {
1080+
promiseCompleted = true;
1081+
});
1082+
$httpBackend.flush();
1083+
expect(promiseCompleted).toBe(true);
1084+
});
1085+
1086+
describe('when invokeApply is defined and falsy', function() {
1087+
it('should not $apply after success callback', function() {
1088+
$httpBackend.when('GET').respond(200);
1089+
$http({method: 'GET', url: '/some'}, false);
1090+
$httpBackend.flush();
1091+
expect($rootScope.$apply).not.toHaveBeenCalledOnce();
1092+
});
1093+
1094+
1095+
it('should not $apply after error callback', function() {
1096+
$httpBackend.when('GET').respond(404);
1097+
$http({method: 'GET', url: '/some'}, false).catch(noop);
1098+
$httpBackend.flush();
1099+
expect($rootScope.$apply).not.toHaveBeenCalledOnce();
1100+
});
1101+
1102+
it('should not $apply if exception thrown during callback', inject(function($exceptionHandler) {
1103+
$httpBackend.when('GET').respond(200);
1104+
callback.and.throwError('error in callback');
1105+
1106+
$http({method: 'GET', url: '/some'}, false).then(callback);
1107+
$httpBackend.flush();
1108+
expect($rootScope.$apply).not.toHaveBeenCalledOnce();
1109+
1110+
$exceptionHandler.errors = [];
1111+
}));
1112+
});
10741113
});
10751114

10761115

0 commit comments

Comments
 (0)