repackaging: all the file moves
This commit is contained in:
@ -0,0 +1,48 @@
|
||||
library angular2.src.services.xhr_cache;
|
||||
|
||||
import 'dart:async' show Future;
|
||||
import 'dart:html';
|
||||
import 'dart:js' as js;
|
||||
import 'package:angular2/core.dart';
|
||||
import 'package:angular2/src/compiler/xhr.dart';
|
||||
import 'package:angular2/src/facade/exceptions.dart' show BaseException;
|
||||
|
||||
/**
|
||||
* An implementation of XHR that uses a template cache to avoid doing an actual
|
||||
* XHR.
|
||||
*
|
||||
* The template cache needs to be built and loaded into window.$templateCache
|
||||
* via a separate mechanism.
|
||||
*/
|
||||
@Injectable()
|
||||
class CachedXHR extends XHR {
|
||||
js.JsObject _cache;
|
||||
String _baseUri;
|
||||
|
||||
CachedXHR() {
|
||||
if (js.context.hasProperty(r'$templateCache')) {
|
||||
this._cache = js.context[r'$templateCache'];
|
||||
} else {
|
||||
throw new BaseException(
|
||||
r'CachedXHR: Template cache was not found in $templateCache.');
|
||||
}
|
||||
this._baseUri = window.location.protocol +
|
||||
'//' +
|
||||
window.location.host +
|
||||
window.location.pathname;
|
||||
int lastSlash = this._baseUri.lastIndexOf('/');
|
||||
this._baseUri = this._baseUri.substring(0, lastSlash + 1);
|
||||
}
|
||||
|
||||
Future<String> get(String url) {
|
||||
if (url.startsWith(this._baseUri)) {
|
||||
url = url.substring(this._baseUri.length);
|
||||
}
|
||||
if (this._cache.hasProperty(url)) {
|
||||
return new Future.value(this._cache[url]);
|
||||
} else {
|
||||
return new Future.error(
|
||||
'CachedXHR: Did not find cached template for ' + url);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
import {XHR} from 'angular2/src/compiler/xhr';
|
||||
import {BaseException} from 'angular2/src/facade/exceptions';
|
||||
import {global} from 'angular2/src/facade/lang';
|
||||
import {PromiseWrapper} from 'angular2/src/facade/promise';
|
||||
|
||||
/**
|
||||
* An implementation of XHR that uses a template cache to avoid doing an actual
|
||||
* XHR.
|
||||
*
|
||||
* The template cache needs to be built and loaded into window.$templateCache
|
||||
* via a separate mechanism.
|
||||
*/
|
||||
export class CachedXHR extends XHR {
|
||||
private _cache: {[url: string]: string};
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this._cache = (<any>global).$templateCache;
|
||||
if (this._cache == null) {
|
||||
throw new BaseException('CachedXHR: Template cache was not found in $templateCache.');
|
||||
}
|
||||
}
|
||||
|
||||
get(url: string): Promise<string> {
|
||||
if (this._cache.hasOwnProperty(url)) {
|
||||
return PromiseWrapper.resolve(this._cache[url]);
|
||||
} else {
|
||||
return PromiseWrapper.reject('CachedXHR: Did not find cached template for ' + url, null);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
library angular2.src.services.xhr_impl;
|
||||
|
||||
import 'dart:async' show Future;
|
||||
import 'dart:html' show HttpRequest;
|
||||
import 'package:angular2/core.dart';
|
||||
import 'package:angular2/src/compiler/xhr.dart';
|
||||
|
||||
@Injectable()
|
||||
class XHRImpl extends XHR {
|
||||
Future<String> get(String url) {
|
||||
return HttpRequest.request(url).then((HttpRequest req) => req.responseText,
|
||||
onError: (_) => new Future.error('Failed to load $url'));
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
import {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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user