feat(compiler): Add an implementation for XHR that uses a template cache to load template files.
Useful for avoiding doing an actual XHR during testing. Part of the solution for #4051 (Other part is a Karma plugin that will create the template cache). Closes #7940
This commit is contained in:

committed by
vikerman

parent
6cbf99086e
commit
a596b887ff
48
modules/angular2/src/platform/browser/xhr_cache.dart
Normal file
48
modules/angular2/src/platform/browser/xhr_cache.dart
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
31
modules/angular2/src/platform/browser/xhr_cache.ts
Normal file
31
modules/angular2/src/platform/browser/xhr_cache.ts
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
import {CONST_EXPR, IS_DART} from 'angular2/src/facade/lang';
|
||||
import {provide, Provider, Injector, OpaqueToken} from 'angular2/src/core/di';
|
||||
|
||||
import {XHR} from 'angular2/src/compiler/xhr';
|
||||
import {
|
||||
PLATFORM_INITIALIZER,
|
||||
PLATFORM_DIRECTIVES,
|
||||
@ -28,6 +28,7 @@ import {BrowserDetails} from "angular2/src/animate/browser_details";
|
||||
import {AnimationBuilder} from "angular2/src/animate/animation_builder";
|
||||
import {BrowserDomAdapter} from './browser/browser_adapter';
|
||||
import {BrowserGetTestability} from 'angular2/src/platform/browser/testability';
|
||||
import {CachedXHR} from 'angular2/src/platform/browser/xhr_cache';
|
||||
import {wtfInit} from 'angular2/src/core/profile/wtf_init';
|
||||
import {EventManager, EVENT_MANAGER_PLUGINS} from "angular2/src/platform/dom/events/event_manager";
|
||||
import {
|
||||
@ -94,6 +95,9 @@ export const BROWSER_APP_COMMON_PROVIDERS: Array<any /*Type | Provider | any[]*/
|
||||
ELEMENT_PROBE_PROVIDERS
|
||||
]);
|
||||
|
||||
export const CACHED_TEMPLATE_PROVIDER: Array<any /*Type | Provider | any[]*/> =
|
||||
CONST_EXPR([new Provider(XHR, {useClass: CachedXHR})]);
|
||||
|
||||
export function initDomAdapter() {
|
||||
BrowserDomAdapter.makeCurrent();
|
||||
wtfInit();
|
||||
|
Reference in New Issue
Block a user