| 
									
										
										
										
											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-08-17 09:24:44 -07:00
										 |  |  | import {ResourceLoader} from '@angular/compiler'; | 
					
						
							| 
									
										
										
										
											2017-03-01 15:18:10 -08:00
										 |  |  | import {ɵglobal as global} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-04-06 15:58:23 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |  * An implementation of ResourceLoader that uses a template cache to avoid doing an actual | 
					
						
							|  |  |  |  * ResourceLoader. | 
					
						
							| 
									
										
										
										
											2016-04-06 15:58:23 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * The template cache needs to be built and loaded into window.$templateCache | 
					
						
							|  |  |  |  * via a separate mechanism. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  | export class CachedResourceLoader extends ResourceLoader { | 
					
						
							| 
									
										
										
										
											2016-04-06 15:58:23 -07:00
										 |  |  |   private _cache: {[url: string]: string}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor() { | 
					
						
							|  |  |  |     super(); | 
					
						
							|  |  |  |     this._cache = (<any>global).$templateCache; | 
					
						
							|  |  |  |     if (this._cache == null) { | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |       throw new Error('CachedResourceLoader: Template cache was not found in $templateCache.'); | 
					
						
							| 
									
										
										
										
											2016-04-06 15:58:23 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   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-17 09:24:44 -07:00
										 |  |  |       return <Promise<any>>Promise.reject( | 
					
						
							|  |  |  |           'CachedResourceLoader: Did not find cached template for ' + url); | 
					
						
							| 
									
										
										
										
											2016-04-06 15:58:23 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |