2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
|
*/
|
|
|
|
|
|
2016-04-28 17:50:03 -07:00
|
|
|
import {XHR} from '@angular/compiler';
|
2016-08-10 15:55:18 -07:00
|
|
|
import {BaseException} from '@angular/core';
|
2016-05-31 15:22:59 -07:00
|
|
|
import {global} from '../facade/lang';
|
2016-04-06 15:58:23 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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)) {
|
2016-08-02 15:53:34 -07:00
|
|
|
return Promise.resolve(this._cache[url]);
|
2016-04-06 15:58:23 -07:00
|
|
|
} else {
|
2016-08-02 15:53:34 -07:00
|
|
|
return <Promise<any>>Promise.reject('CachedXHR: Did not find cached template for ' + url);
|
2016-04-06 15:58:23 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|