Skip to content

Commit 5725f71

Browse files
0x-r4bbitjeffbcross
authored andcommitted
fix(http): allow using JSONP_INJECTABLES and HTTP_INJECTABLES in same injector
Fixes angular#3365 Closes angular#3390
1 parent 88a5b8d commit 5725f71

File tree

2 files changed

+73
-8
lines changed

2 files changed

+73
-8
lines changed

modules/http/http.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,26 @@ export {URLSearchParams} from './src/url_search_params';
6262
*
6363
*/
6464
export const HTTP_BINDINGS: List<any> = [
65-
bind(ConnectionBackend)
66-
.toClass(XHRBackend),
65+
// TODO(pascal): use factory type annotations once supported in DI
66+
// issue: https://github.com/angular/angular/issues/3183
67+
bind(Http)
68+
.toFactory((xhrBackend, requestOptions) => { return new Http(xhrBackend, requestOptions);},
69+
[XHRBackend, RequestOptions]),
6770
BrowserXhr,
6871
bind(RequestOptions).toClass(BaseRequestOptions),
6972
bind(ResponseOptions).toClass(BaseResponseOptions),
70-
Http
73+
XHRBackend
7174
];
7275

7376
export const JSONP_BINDINGS: List<any> = [
74-
bind(ConnectionBackend)
75-
.toClass(JSONPBackend),
77+
// TODO(pascal): use factory type annotations once supported in DI
78+
// issue: https://github.com/angular/angular/issues/3183
79+
bind(Jsonp)
80+
.toFactory(
81+
(jsonpBackend, requestOptions) => { return new Jsonp(jsonpBackend, requestOptions);},
82+
[JSONPBackend, RequestOptions]),
7683
BrowserJsonp,
7784
bind(RequestOptions).toClass(BaseRequestOptions),
7885
bind(ResponseOptions).toClass(BaseResponseOptions),
79-
Jsonp
86+
JSONPBackend
8087
];

modules/http/test/http_spec.ts

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
1717
import {
1818
BaseRequestOptions,
1919
ConnectionBackend,
20-
Http,
2120
Request,
2221
RequestMethods,
2322
RequestOptions,
2423
Response,
2524
ResponseOptions,
26-
URLSearchParams
25+
URLSearchParams,
26+
JSONP_BINDINGS,
27+
HTTP_BINDINGS,
28+
XHRBackend,
29+
JSONPBackend,
30+
Http,
31+
Jsonp
2732
} from 'http/http';
2833

2934
class SpyObserver extends SpyObject {
@@ -39,6 +44,59 @@ class SpyObserver extends SpyObject {
3944
}
4045

4146
export function main() {
47+
describe('injectables', () => {
48+
var url = 'http://foo.bar';
49+
var http: Http;
50+
var parentInjector: Injector;
51+
var childInjector: Injector;
52+
var jsonpBackend: MockBackend;
53+
var xhrBackend: MockBackend;
54+
var jsonp: Jsonp;
55+
var http: Http;
56+
57+
it('should allow using jsonpInjectables and httpInjectables in same injector',
58+
inject([AsyncTestCompleter], (async) => {
59+
parentInjector = Injector.resolveAndCreate(
60+
[bind(XHRBackend).toClass(MockBackend), bind(JSONPBackend).toClass(MockBackend)]);
61+
62+
childInjector = parentInjector.resolveAndCreateChild([
63+
HTTP_BINDINGS,
64+
JSONP_BINDINGS,
65+
bind(XHRBackend).toClass(MockBackend),
66+
bind(JSONPBackend).toClass(MockBackend)
67+
]);
68+
69+
http = childInjector.get(Http);
70+
jsonp = childInjector.get(Jsonp);
71+
jsonpBackend = childInjector.get(JSONPBackend);
72+
xhrBackend = childInjector.get(XHRBackend);
73+
74+
var xhrCreatedConnections = 0;
75+
var jsonpCreatedConnections = 0;
76+
77+
78+
ObservableWrapper.subscribe(xhrBackend.connections, () => {
79+
xhrCreatedConnections++;
80+
expect(xhrCreatedConnections).toEqual(1);
81+
if (jsonpCreatedConnections) {
82+
async.done();
83+
}
84+
});
85+
86+
ObservableWrapper.subscribe(http.get(url), () => {});
87+
88+
ObservableWrapper.subscribe(jsonpBackend.connections, () => {
89+
jsonpCreatedConnections++;
90+
expect(jsonpCreatedConnections).toEqual(1);
91+
if (xhrCreatedConnections) {
92+
async.done();
93+
}
94+
});
95+
96+
ObservableWrapper.subscribe(jsonp.request(url), () => {});
97+
}));
98+
});
99+
42100
describe('http', () => {
43101
var url = 'http://foo.bar';
44102
var http: Http;

0 commit comments

Comments
 (0)