From 9e8d31d5324b9909539889f4a8d12fb2f616847f Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Thu, 30 Apr 2015 15:27:28 -0700 Subject: [PATCH] fix(compiler): clone templates before compiling them This is needed as the compiler changes templates during compilation and we are caching templates in the `TemplateLoader`. Closes #1058 --- modules/angular2/src/render/dom/compiler/template_loader.js | 4 +++- .../test/render/dom/compiler/template_loader_spec.js | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/modules/angular2/src/render/dom/compiler/template_loader.js b/modules/angular2/src/render/dom/compiler/template_loader.js index 09924d03b3..5a57615b07 100644 --- a/modules/angular2/src/render/dom/compiler/template_loader.js +++ b/modules/angular2/src/render/dom/compiler/template_loader.js @@ -39,7 +39,9 @@ export class TemplateLoader { StringMapWrapper.set(this._htmlCache, url, promise); } - return promise; + // We need to clone the result as others might change it + // (e.g. the compiler). + return promise.then( (tplElement) => DOM.clone(tplElement) ); } throw new BaseException('View should have either the url or template property set'); diff --git a/modules/angular2/test/render/dom/compiler/template_loader_spec.js b/modules/angular2/test/render/dom/compiler/template_loader_spec.js index d11196a12f..ba290d187f 100644 --- a/modules/angular2/test/render/dom/compiler/template_loader_spec.js +++ b/modules/angular2/test/render/dom/compiler/template_loader_spec.js @@ -45,17 +45,19 @@ export function main() { xhr.flush(); })); - it('should cache template loaded through XHR', inject([AsyncTestCompleter], (async) => { + it('should cache template loaded through XHR but clone it as the compiler might change it', inject([AsyncTestCompleter], (async) => { var firstEl; + // we have only one xhr.expect, so there can only be one xhr call! xhr.expect('base/foo', 'xhr template'); var template = new ViewDefinition({absUrl: 'base/foo'}); loader.load(template) .then((el) => { + expect(DOM.content(el)).toHaveText('xhr template'); firstEl = el; return loader.load(template); }) .then((el) =>{ - expect(el).toBe(firstEl); + expect(el).not.toBe(firstEl); expect(DOM.content(el)).toHaveText('xhr template'); async.done(); });