Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat ($http): Add custom createXhr factory to config object #9319

Closed
wants to merge 1 commit into from

Conversation

sjurba
Copy link
Contributor

@sjurba sjurba commented Sep 28, 2014

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

@sjurba
Copy link
Contributor Author

sjurba commented Sep 28, 2014

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.
It would be good to get some feedback on whether this is required/desirable.

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.

@caitp
Copy link
Contributor

caitp commented Sep 29, 2014

can you get the ngMockE2E test passing again? thanks

@sjurba
Copy link
Contributor Author

sjurba commented Sep 29, 2014

Yeah, sry bout that.. Should be working again now.

@caitp
Copy link
Contributor

caitp commented Sep 29, 2014

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
Copy link
Contributor Author

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.

@sjurba
Copy link
Contributor Author

sjurba commented Sep 29, 2014

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);
Copy link
Contributor

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?

@sjurba
Copy link
Contributor Author

sjurba commented Sep 29, 2014

The alternative is in the first commit sjurba@978e8b1

Adding yet another parameter..

@pkozlowski-opensource
Copy link
Member

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.

@pkozlowski-opensource pkozlowski-opensource self-assigned this Jan 21, 2015
@coolaj86
Copy link

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

@DaAwesomeP
Copy link

+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.

@frekele
Copy link

frekele commented Jun 8, 2015

+++1 !!!!!

@caitp
Copy link
Contributor

caitp commented Jun 8, 2015

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

@caitp caitp modified the milestones: 1.4.1, Backlog Jun 8, 2015
@pkozlowski-opensource
Copy link
Member

OK, let's review this one! I will send an alternative proposal and then we will compare / decide.

@sjurba
Copy link
Contributor Author

sjurba commented Jun 13, 2015

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.

@pkozlowski-opensource
Copy link
Member

@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.

@gkalpak
Copy link
Member

gkalpak commented Jun 15, 2015

👍 for not overloading the config object. $http's config IS the new DDO.

(I mean, I don't have to look up DDO properties any more (since several months now), but I still have to look up $http's config every time 😄)

@pkozlowski-opensource
Copy link
Member

@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

@gkalpak
Copy link
Member

gkalpak commented Jun 15, 2015

@pkozlowski-opensource, I like the general idea (although your implementation has some bugs 😛).

@pkozlowski-opensource
Copy link
Member

@gkalpak this is "pseudo implementation" :-) Want to confirm the approach before investing more than 5 minutes into it :-)

@sjurba
Copy link
Contributor Author

sjurba commented Jun 15, 2015

@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.

@pkozlowski-opensource
Copy link
Member

@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!

@DaAwesomeP
Copy link

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mozSystem parameter for window.XMLHttpRequest on FirefoxOS