
Currently, core depends on the browser, which means that other platforms (e.g., NativeScript or webworker) cannot use the bootstrapping logic core provides. This PR extract makes bootstrapping logic in core completely platform-independent. The browser-specific code was moved to "angular2/platforms/browser". BREAKING CHANGE A few private helpers (e.g., platformCommon or applicationCommon) were removed or replaced with other helpers. Look at PLATFORM_COMMON_PROVIDERS, APPLICATION_COMMON_PROVIDERS, BROWSER_PROVIDERS, BROWSER_APP_PROVIDERS to see if they export the providers you need. Closes #5219 Closes #5280
40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
import {Promise, PromiseWrapper, PromiseCompleter} from 'angular2/src/facade/promise';
|
|
import {isPresent} from 'angular2/src/facade/lang';
|
|
import {XHR} from 'angular2/src/compiler/xhr';
|
|
|
|
export class XHRImpl extends XHR {
|
|
get(url: string): Promise<string> {
|
|
var completer: PromiseCompleter < string >= PromiseWrapper.completer();
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.open('GET', url, true);
|
|
xhr.responseType = 'text';
|
|
|
|
xhr.onload = function() {
|
|
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
|
|
// response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
|
|
var response = isPresent(xhr.response) ? xhr.response : xhr.responseText;
|
|
|
|
// normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
|
|
var status = xhr.status === 1223 ? 204 : xhr.status;
|
|
|
|
// fix status code when it is 0 (0 status is undocumented).
|
|
// Occurs when accessing file resources or on Android 4.1 stock browser
|
|
// while retrieving files from application cache.
|
|
if (status === 0) {
|
|
status = response ? 200 : 0;
|
|
}
|
|
|
|
if (200 <= status && status <= 300) {
|
|
completer.resolve(response);
|
|
} else {
|
|
completer.reject(`Failed to load ${url}`, null);
|
|
}
|
|
};
|
|
|
|
xhr.onerror = function() { completer.reject(`Failed to load ${url}`, null); };
|
|
|
|
xhr.send();
|
|
return completer.promise;
|
|
}
|
|
}
|