diff --git a/platform/representation/bundle.json b/platform/representation/bundle.json index 7d6ddd3113..8b185422b9 100644 --- a/platform/representation/bundle.json +++ b/platform/representation/bundle.json @@ -52,7 +52,7 @@ { "key": "templateLinker", "implementation": "TemplateLinker.js", - "depends": [ "$http", "$compile", "$log" ], + "depends": [ "$templateRequest", "$sce", "$compile", "$log" ], "comment": "For internal use by mct-include and mct-representation." } ], diff --git a/platform/representation/src/TemplateLinker.js b/platform/representation/src/TemplateLinker.js index da357fc1e3..b462afa626 100644 --- a/platform/representation/src/TemplateLinker.js +++ b/platform/representation/src/TemplateLinker.js @@ -39,26 +39,18 @@ define( * @param $log Angular's `$log` service * @private */ - function TemplateLinker($http, $compile, $log) { - this.templateMap = {}; - this.$http = $http; + function TemplateLinker($templateRequest, $sce, $compile, $log) { + this.$templateRequest = $templateRequest; + this.$sce = $sce; this.$compile = $compile; this.$log = $log; } TemplateLinker.prototype.load = function (templateUrl) { - var $http = this.$http, - $compile = this.$compile, - $log = this.$log, - templateMap = this.templateMap; - - return (templateMap[templateUrl] = templateMap[templateUrl] || - $http.get(templateUrl).then(function (response) { - return $compile(response.data); - }, function () { - $log.warn("Couldn't load template at " + templateUrl); - templateMap[templateUrl] = undefined; - })); + return this.$templateRequest( + this.$sce.trustAsResourceUrl(templateUrl), + false + ); }; /** @@ -108,18 +100,13 @@ define( } function populateElement(template) { - template(scope, function (innerClone) { - element.empty(); - element.append(innerClone); - }); + element.empty(); + element.append(self.$compile(template)(scope)); } - function applyTemplate(template, templateUrl) { - if (template) { - populateElement(template); - } else { - removeElement(); - } + function badTemplate(templateUrl) { + self.$log.warn("Couldn't load template at " + templateUrl); + removeElement(); } function changeTemplate(templateUrl) { @@ -129,8 +116,10 @@ define( self.load(templateUrl).then(function (template) { // Avoid race conditions if (templateUrl === activeTemplateUrl) { - applyTemplate(template); + populateElement(template); } + }, function () { + badTemplate(templateUrl); }); } else { removeElement(); diff --git a/platform/representation/test/TemplateLinkerSpec.js b/platform/representation/test/TemplateLinkerSpec.js index 38a3f867e2..bf762fba53 100644 --- a/platform/representation/test/TemplateLinkerSpec.js +++ b/platform/representation/test/TemplateLinkerSpec.js @@ -30,30 +30,29 @@ define( var JQLITE_METHODS = [ 'replaceWith', 'empty', 'append' ]; describe("TemplateLinker", function () { - var mockHttp, + var mockTemplateRequest, + mockSce, mockCompile, mockLog, mockScope, mockElement, mockTemplates, mockElements, - mockHttpPromise, - mockChainPromise, + mockPromise, linker; beforeEach(function () { - mockHttp = jasmine.createSpyObj('$http', ['get']); + mockTemplateRequest = jasmine.createSpy('$templateRequest'); + mockSce = jasmine.createSpyObj('$sce', ['trustAsResourceUrl']); mockCompile = jasmine.createSpy('$compile'); mockLog = jasmine.createSpyObj('$log', ['error', 'warn']); mockScope = jasmine.createSpyObj('$scope', ['$on']); mockElement = jasmine.createSpyObj('element', JQLITE_METHODS); - mockHttpPromise = jasmine.createSpyObj('promise1', ['then']); - mockChainPromise = jasmine.createSpyObj('promise2', ['then']); + mockPromise = jasmine.createSpyObj('promise', ['then']); mockTemplates = {}; mockElements = {}; - mockHttp.get.andReturn(mockHttpPromise); - mockHttpPromise.then.andReturn(mockChainPromise); + mockTemplateRequest.andReturn(mockPromise); mockCompile.andCallFake(function (html) { mockTemplates[html] = jasmine.createSpy('template'); mockElements[html] = @@ -61,9 +60,13 @@ define( mockTemplates[html].andReturn(mockElements[html]); return mockTemplates[html]; }); + mockSce.trustAsResourceUrl.andCallFake(function (url) { + return { trusted: url }; + }); linker = new TemplateLinker( - mockHttp, + mockTemplateRequest, + mockSce, mockCompile, mockLog ); @@ -118,44 +121,28 @@ define( testUrl = "some/url/template.html"; testTemplate = "