mirror of
https://github.com/nasa/openmct.git
synced 2025-02-21 09:52:04 +00:00
[Representation] Use $templateRequest
...from templateLinker, to remove the need to use $http and to explicitly cache templates.
This commit is contained in:
parent
5ed34c1c30
commit
d5f1d45759
@ -52,7 +52,7 @@
|
|||||||
{
|
{
|
||||||
"key": "templateLinker",
|
"key": "templateLinker",
|
||||||
"implementation": "TemplateLinker.js",
|
"implementation": "TemplateLinker.js",
|
||||||
"depends": [ "$http", "$compile", "$log" ],
|
"depends": [ "$templateRequest", "$sce", "$compile", "$log" ],
|
||||||
"comment": "For internal use by mct-include and mct-representation."
|
"comment": "For internal use by mct-include and mct-representation."
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -39,26 +39,18 @@ define(
|
|||||||
* @param $log Angular's `$log` service
|
* @param $log Angular's `$log` service
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
function TemplateLinker($http, $compile, $log) {
|
function TemplateLinker($templateRequest, $sce, $compile, $log) {
|
||||||
this.templateMap = {};
|
this.$templateRequest = $templateRequest;
|
||||||
this.$http = $http;
|
this.$sce = $sce;
|
||||||
this.$compile = $compile;
|
this.$compile = $compile;
|
||||||
this.$log = $log;
|
this.$log = $log;
|
||||||
}
|
}
|
||||||
|
|
||||||
TemplateLinker.prototype.load = function (templateUrl) {
|
TemplateLinker.prototype.load = function (templateUrl) {
|
||||||
var $http = this.$http,
|
return this.$templateRequest(
|
||||||
$compile = this.$compile,
|
this.$sce.trustAsResourceUrl(templateUrl),
|
||||||
$log = this.$log,
|
false
|
||||||
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;
|
|
||||||
}));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -108,18 +100,13 @@ define(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function populateElement(template) {
|
function populateElement(template) {
|
||||||
template(scope, function (innerClone) {
|
element.empty();
|
||||||
element.empty();
|
element.append(self.$compile(template)(scope));
|
||||||
element.append(innerClone);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyTemplate(template, templateUrl) {
|
function badTemplate(templateUrl) {
|
||||||
if (template) {
|
self.$log.warn("Couldn't load template at " + templateUrl);
|
||||||
populateElement(template);
|
removeElement();
|
||||||
} else {
|
|
||||||
removeElement();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function changeTemplate(templateUrl) {
|
function changeTemplate(templateUrl) {
|
||||||
@ -129,8 +116,10 @@ define(
|
|||||||
self.load(templateUrl).then(function (template) {
|
self.load(templateUrl).then(function (template) {
|
||||||
// Avoid race conditions
|
// Avoid race conditions
|
||||||
if (templateUrl === activeTemplateUrl) {
|
if (templateUrl === activeTemplateUrl) {
|
||||||
applyTemplate(template);
|
populateElement(template);
|
||||||
}
|
}
|
||||||
|
}, function () {
|
||||||
|
badTemplate(templateUrl);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
removeElement();
|
removeElement();
|
||||||
|
@ -30,30 +30,29 @@ define(
|
|||||||
var JQLITE_METHODS = [ 'replaceWith', 'empty', 'append' ];
|
var JQLITE_METHODS = [ 'replaceWith', 'empty', 'append' ];
|
||||||
|
|
||||||
describe("TemplateLinker", function () {
|
describe("TemplateLinker", function () {
|
||||||
var mockHttp,
|
var mockTemplateRequest,
|
||||||
|
mockSce,
|
||||||
mockCompile,
|
mockCompile,
|
||||||
mockLog,
|
mockLog,
|
||||||
mockScope,
|
mockScope,
|
||||||
mockElement,
|
mockElement,
|
||||||
mockTemplates,
|
mockTemplates,
|
||||||
mockElements,
|
mockElements,
|
||||||
mockHttpPromise,
|
mockPromise,
|
||||||
mockChainPromise,
|
|
||||||
linker;
|
linker;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
mockHttp = jasmine.createSpyObj('$http', ['get']);
|
mockTemplateRequest = jasmine.createSpy('$templateRequest');
|
||||||
|
mockSce = jasmine.createSpyObj('$sce', ['trustAsResourceUrl']);
|
||||||
mockCompile = jasmine.createSpy('$compile');
|
mockCompile = jasmine.createSpy('$compile');
|
||||||
mockLog = jasmine.createSpyObj('$log', ['error', 'warn']);
|
mockLog = jasmine.createSpyObj('$log', ['error', 'warn']);
|
||||||
mockScope = jasmine.createSpyObj('$scope', ['$on']);
|
mockScope = jasmine.createSpyObj('$scope', ['$on']);
|
||||||
mockElement = jasmine.createSpyObj('element', JQLITE_METHODS);
|
mockElement = jasmine.createSpyObj('element', JQLITE_METHODS);
|
||||||
mockHttpPromise = jasmine.createSpyObj('promise1', ['then']);
|
mockPromise = jasmine.createSpyObj('promise', ['then']);
|
||||||
mockChainPromise = jasmine.createSpyObj('promise2', ['then']);
|
|
||||||
mockTemplates = {};
|
mockTemplates = {};
|
||||||
mockElements = {};
|
mockElements = {};
|
||||||
|
|
||||||
mockHttp.get.andReturn(mockHttpPromise);
|
mockTemplateRequest.andReturn(mockPromise);
|
||||||
mockHttpPromise.then.andReturn(mockChainPromise);
|
|
||||||
mockCompile.andCallFake(function (html) {
|
mockCompile.andCallFake(function (html) {
|
||||||
mockTemplates[html] = jasmine.createSpy('template');
|
mockTemplates[html] = jasmine.createSpy('template');
|
||||||
mockElements[html] =
|
mockElements[html] =
|
||||||
@ -61,9 +60,13 @@ define(
|
|||||||
mockTemplates[html].andReturn(mockElements[html]);
|
mockTemplates[html].andReturn(mockElements[html]);
|
||||||
return mockTemplates[html];
|
return mockTemplates[html];
|
||||||
});
|
});
|
||||||
|
mockSce.trustAsResourceUrl.andCallFake(function (url) {
|
||||||
|
return { trusted: url };
|
||||||
|
});
|
||||||
|
|
||||||
linker = new TemplateLinker(
|
linker = new TemplateLinker(
|
||||||
mockHttp,
|
mockTemplateRequest,
|
||||||
|
mockSce,
|
||||||
mockCompile,
|
mockCompile,
|
||||||
mockLog
|
mockLog
|
||||||
);
|
);
|
||||||
@ -118,44 +121,28 @@ define(
|
|||||||
testUrl = "some/url/template.html";
|
testUrl = "some/url/template.html";
|
||||||
testTemplate = "<div>Some template!</div>";
|
testTemplate = "<div>Some template!</div>";
|
||||||
changeTemplate(testUrl);
|
changeTemplate(testUrl);
|
||||||
|
mockPromise.then.mostRecentCall
|
||||||
|
.args[0](testTemplate);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("loads templates using $http", function () {
|
it("loads templates using $templateRequest", function () {
|
||||||
expect(mockHttp.get).toHaveBeenCalledWith(testUrl);
|
expect(mockTemplateRequest).toHaveBeenCalledWith({
|
||||||
|
trusted: testUrl
|
||||||
|
}, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("compiles loaded templates with linked scope", function () {
|
it("compiles loaded templates with linked scope", function () {
|
||||||
var chainValue;
|
|
||||||
chainValue = mockHttpPromise.then.mostRecentCall.args[0]({
|
|
||||||
data: testTemplate
|
|
||||||
});
|
|
||||||
expect(mockCompile).toHaveBeenCalledWith(testTemplate);
|
expect(mockCompile).toHaveBeenCalledWith(testTemplate);
|
||||||
mockChainPromise.then.mostRecentCall.args[0](chainValue);
|
expect(mockTemplates[testTemplate])
|
||||||
expect(mockTemplates[testTemplate]).toHaveBeenCalledWith(
|
.toHaveBeenCalledWith(mockScope);
|
||||||
mockScope,
|
|
||||||
jasmine.any(Function)
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("replaces comments with specified element", function () {
|
it("replaces comments with specified element", function () {
|
||||||
mockChainPromise.then.mostRecentCall.args[0](
|
|
||||||
mockHttpPromise.then.mostRecentCall.args[0]({
|
|
||||||
data: testTemplate
|
|
||||||
})
|
|
||||||
);
|
|
||||||
expect(commentElement.replaceWith)
|
expect(commentElement.replaceWith)
|
||||||
.toHaveBeenCalledWith(mockElement);
|
.toHaveBeenCalledWith(mockElement);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("appends rendered content to the specified element", function () {
|
it("appends rendered content to the specified element", function () {
|
||||||
mockChainPromise.then.mostRecentCall.args[0](
|
|
||||||
mockHttpPromise.then.mostRecentCall.args[0]({
|
|
||||||
data: testTemplate
|
|
||||||
})
|
|
||||||
);
|
|
||||||
mockTemplates[testTemplate].mostRecentCall.args[1](
|
|
||||||
mockElements[testTemplate]
|
|
||||||
);
|
|
||||||
expect(mockElement.append)
|
expect(mockElement.append)
|
||||||
.toHaveBeenCalledWith(mockElements[testTemplate]);
|
.toHaveBeenCalledWith(mockElements[testTemplate]);
|
||||||
});
|
});
|
||||||
@ -163,11 +150,6 @@ define(
|
|||||||
it("clears templates when called with undefined", function () {
|
it("clears templates when called with undefined", function () {
|
||||||
expect(mockElement.replaceWith.callCount)
|
expect(mockElement.replaceWith.callCount)
|
||||||
.toEqual(1);
|
.toEqual(1);
|
||||||
mockChainPromise.then.mostRecentCall.args[0](
|
|
||||||
mockHttpPromise.then.mostRecentCall.args[0]({
|
|
||||||
data: testTemplate
|
|
||||||
})
|
|
||||||
);
|
|
||||||
changeTemplate(undefined);
|
changeTemplate(undefined);
|
||||||
expect(mockElement.replaceWith.callCount)
|
expect(mockElement.replaceWith.callCount)
|
||||||
.toEqual(2);
|
.toEqual(2);
|
||||||
@ -175,24 +157,15 @@ define(
|
|||||||
.toEqual(commentElement);
|
.toEqual(commentElement);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("logs no warnings", function () {
|
it("logs no warnings for nominal changes", function () {
|
||||||
mockChainPromise.then.mostRecentCall.args[0](
|
|
||||||
mockHttpPromise.then.mostRecentCall.args[0]({
|
|
||||||
data: testTemplate
|
|
||||||
})
|
|
||||||
);
|
|
||||||
mockTemplates[testTemplate].mostRecentCall.args[1](
|
|
||||||
mockElements[testTemplate]
|
|
||||||
);
|
|
||||||
expect(mockLog.warn).not.toHaveBeenCalled();
|
expect(mockLog.warn).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("which cannot be found", function () {
|
describe("which cannot be found", function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
mockChainPromise.then.mostRecentCall.args[0](
|
changeTemplate("some/bad/url");
|
||||||
// Reject the http promise
|
// Reject the template promise
|
||||||
mockHttpPromise.then.mostRecentCall.args[1]()
|
mockPromise.then.mostRecentCall.args[1]();
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("removes the element from the DOM", function () {
|
it("removes the element from the DOM", function () {
|
||||||
@ -227,25 +200,12 @@ define(
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("loads the specified template", function () {
|
it("loads the specified template", function () {
|
||||||
expect(mockHttp.get).toHaveBeenCalledWith(testUrl);
|
expect(mockTemplateRequest).toHaveBeenCalledWith({
|
||||||
|
trusted: testUrl
|
||||||
|
}, false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not issue multiple requests for the same URL", function () {
|
|
||||||
var testUrls = ['a', 'b', 'c', 'd'].map(function (k) {
|
|
||||||
return k + "/some/template.html";
|
|
||||||
});
|
|
||||||
|
|
||||||
testUrls.forEach(function (testUrl, i) {
|
|
||||||
expect(mockHttp.get.callCount).toEqual(i);
|
|
||||||
linker.link(mockScope, mockElement, testUrl);
|
|
||||||
expect(mockHttp.get.callCount).toEqual(i + 1);
|
|
||||||
linker.link(mockScope, mockElement, testUrl);
|
|
||||||
expect(mockHttp.get.callCount).toEqual(i + 1);
|
|
||||||
linker.link(mockScope, mockElement)(testUrl);
|
|
||||||
expect(mockHttp.get.callCount).toEqual(i + 1);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user