-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat ($http): Add custom createXhr factory to config object #9319
Conversation
I added to commits to this PR. This first is just a simple implementation of the new config field. In the second I did a bit of refactoring to shorten the list of parameters in a few core functions in $httpBackend. Secondly with the introduction of this config parameter I see the potential of greatly simplifying the process of mocking out $http for ngMock. But this will require a bit more work and it will likely have some wide spreading implications. So I won't do anything about that until I get some feedback on this idea from the angular team. |
can you get the ngMockE2E test passing again? thanks |
Yeah, sry bout that.. Should be working again now. |
so it's more stuff added to the public API surface, which means I can't just say 'lgtm' and merge it, people need to agree that it's what we want to do. I'm not totally sure about letting this be configured per-request, that's not the approach jQuery takes with it. But in general it doesn't look bad to me so far. |
@@ -684,7 +686,8 @@ function $HttpProvider() { | |||
var config = { | |||
method: 'get', | |||
transformRequest: defaults.transformRequest, | |||
transformResponse: defaults.transformResponse | |||
transformResponse: defaults.transformResponse, | |||
createXhr: defaults.createXhr |
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.
You can set it on the globals object as well to configure it globally. I think that's what jquery does as well..
xhr (default: ActiveXObject when available (IE), the XMLHttpRequest otherwise)
Type: Function()
Callback for creating the XMLHttpRequest object.
Defaults to the ActiveXObject when available (IE), the XMLHttpRequest otherwise.
Override to provide your own implementation for XMLHttpRequest or enhancements to the factory.
And,
settings
Type: PlainObject
A set of key/value pairs that configure the Ajax request. All settings are optional.
A default can be set for any option with $.ajaxSetup().
See jQuery.ajax( settings ) below for a complete list of all settings.
You have any comments regarding using this for mocking? |
@@ -990,8 +993,7 @@ function $HttpProvider() { | |||
reqHeaders[(config.xsrfHeaderName || defaults.xsrfHeaderName)] = xsrfValue; | |||
} | |||
|
|||
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout, | |||
config.withCredentials, config.responseType); |
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.
even though this is smaller and saner, i'm not sure if we can actually make this change. @jeffbcross wdyt?
The alternative is in the first commit sjurba@978e8b1 Adding yet another parameter.. |
e8dc429
to
e83fab9
Compare
4dd5a20
to
998c61c
Compare
I was recently looking at the issues around $http and I agree that we need some flex point so people can provide they own factory for XHR objects. Having said this I agree with @caitp that having per-request attribute in a config object is kind of... I don't know... simply doesn't sound good to me. Why not have a setting / function on the $httpBackend level that people could override / customise? Such a function would be passed a config object and return the XHR object. Basically moving createXhr to be "public" / overridable. |
Bump. #fxos still needs this. See http://stackoverflow.com/a/29960399/151312 It's an easy enough workaround, but there are a decent number of newbies for whom it adds significant barrier to entry for using angular on Firefox OS. Coding isn't just for grown-ups anymore: https://www.youtube.com/watch?v=aH9Plt77cjM |
+1 I install Angular via bower and modifying the file after bower installs it is inconvenient and bower_components shouldn't be saved to a git repository anyways. |
+++1 !!!!! |
yeah, sorry this seems to have fell off the radar, I'll try to bring this up in the meeting tomorrow and put this back on track for landing |
OK, let's review this one! I will send an alternative proposal and then we will compare / decide. |
I rebased this one onto master. I also removed the refactoring of the httpbackend arguments since from the feedback it didn't seem like something we could do. So this is straight up attempt at adding an extra parameter to the config object. If the new parameter should only be available configurable on the global it's a simple fix to move it out of the extend function and always overwrite it. However if this is the way to go, there is maybe a simpler solution. Do we have/can we get access to the defaults object in the httpBackend somehow? If so it might simplify the implementation a bit. I'm more than happy to contribute, but I'll need your guidance. |
@sjurba we've discussed this PR during out last team meeting and I would like to run an alternative idea through you / the community. So, I understand what you are trying to do and your approach is simple and effective. Still there is one thing I'm not too keen on - adding more properties to the $http's config object. This makes $http's API surface larger - we've got a config object with gazillion of properties. This starts to resemble the directive definition object and we all know that this is not the nicest API. The alternative idea would be to abstract XHR object creation in a dedicated service (provider). Give me sec, I'm going to open a sketch of the PR with this idea. |
👍 for not overloading the config object. (I mean, I don't have to look up DDO properties any more (since several months now), but I still have to look up |
@sjurba @gkalpak OK, here is an alternative approach: pkozlowski-opensource@249efc5 Didn't test, missing unit-tests and docs etc. but this is just to get the idea across. Please let me know what do you think. //cc: @petebacondarwin |
@pkozlowski-opensource, I like the general idea (although your implementation has some bugs 😛). |
@gkalpak this is "pseudo implementation" :-) Want to confirm the approach before investing more than 5 minutes into it :-) |
@pkozlowski-opensource I like it too. Unless someone else jumps to it, I'll be happy to give it a go when I have some time later this week. |
@sjurba OK, I would be more than happy to let you continue along those lines! Feel free to ping me if you need any info / help! |
Thanks! |
To be able to construct non-standard or customised XHR objects a factory an optional factory function can be added to the config object. This is required for use of the mozAnon and mozSystem options for FirefoxOS.
See https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#XMLHttpRequest() for details.
This is an alternative solution to the one suggested in PR #7903. Closes #2318