-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat($http): add $xhrFactory service to enable creation of custom xhr objects #12159
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,40 @@ | ||
'use strict'; | ||
|
||
function createXhr() { | ||
return new window.XMLHttpRequest(); | ||
/** | ||
* @ngdoc service | ||
* @name $xhrFactory | ||
* | ||
* @description | ||
* Factory function used to create XMLHttpRequest objects. | ||
* | ||
* Replace or decorate this service to create your own custom XMLHttpRequest objects. | ||
* | ||
* ``` | ||
* angular.module('myApp', []) | ||
* .factory('$xhrFactory', function() { | ||
* return function createXhr(method, url) { | ||
* return new window.XMLHttpRequest({mozSystem: true}); | ||
* }; | ||
* }); | ||
* ``` | ||
* | ||
* @param {string} method HTTP method of the request (GET, POST, PUT, ..) | ||
* @param {string} url URL of the request. | ||
*/ | ||
function $xhrFactoryProvider() { | ||
this.$get = function() { | ||
return function createXhr() { | ||
return new window.XMLHttpRequest(); | ||
}; | ||
}; | ||
} | ||
|
||
/** | ||
* @ngdoc service | ||
* @name $httpBackend | ||
* @requires $window | ||
* @requires $document | ||
* @requires $xhrFactory | ||
* | ||
* @description | ||
* HTTP backend used by the {@link ng.$http service} that delegates to | ||
|
@@ -21,8 +47,8 @@ function createXhr() { | |
* $httpBackend} which can be trained with responses. | ||
*/ | ||
function $HttpBackendProvider() { | ||
this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) { | ||
return createHttpBackend($browser, createXhr, $browser.defer, $window.angular.callbacks, $document[0]); | ||
this.$get = ['$browser', '$window', '$document', '$xhrFactory', function($browser, $window, $document, $xhrFactory) { | ||
return createHttpBackend($browser, $xhrFactory, $browser.defer, $window.angular.callbacks, $document[0]); | ||
}]; | ||
} | ||
|
||
|
@@ -46,7 +72,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc | |
}); | ||
} else { | ||
|
||
var xhr = createXhr(); | ||
var xhr = createXhr(method, url); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious: Why are we passing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could pass more (essentially everything we've got here :-)). We can't really predict what people might need... The other approach would be to expose nothing by default and expand the API as soon as we've got more use cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not for us, but for the benefit of end users who will override the service. Let's say you want to use a custom object for certain urls only.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think method and url is a good middle ground to start with. |
||
|
||
xhr.open(method, url, true); | ||
forEach(headers, function(value, key) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This affects $http and $resource requests alike, right? (since resource uses http). We should probably note this here.