[Representation] Build URLs from templateLinker

This commit is contained in:
Victor Woeltjen 2015-10-30 11:02:13 -07:00
parent 5af3d575a2
commit 5ed34c1c30
6 changed files with 59 additions and 19 deletions

View File

@ -71,14 +71,10 @@ define(
// Prepopulate templateMap for easy look up by key // Prepopulate templateMap for easy look up by key
templates.forEach(function (template) { templates.forEach(function (template) {
var key = template.key, var key = template.key;
path = [
template.bundle.path,
template.bundle.resources,
template.templateUrl
].join("/");
// First found should win (priority ordering) // First found should win (priority ordering)
templateMap[key] = templateMap[key] || path; templateMap[key] =
templateMap[key] || templateLinker.getPath(template);
}); });
return { return {

View File

@ -72,11 +72,7 @@ define(
// Get a path to a representation // Get a path to a representation
function getPath(representation) { function getPath(representation) {
return [ return templateLinker.getPath(representation);
representation.bundle.path,
representation.bundle.resources,
representation.templateUrl
].join("/");
} }
// Look up a matching representation for this domain object // Look up a matching representation for this domain object

View File

@ -61,6 +61,20 @@ define(
})); }));
}; };
/**
* Get a path to a template from an extension definition fo
* a template, representation, or view.
* @param {TemplateDefinition} extensionDefinition the definition
* of the template/representation/view to resolve
*/
TemplateLinker.prototype.getPath = function (extensionDefinition) {
return [
extensionDefinition.bundle.path,
extensionDefinition.bundle.resources,
extensionDefinition.templateUrl
].join('/');
};
/** /**
* Populate the given element with templates, within the given scope; * Populate the given element with templates, within the given scope;
* intended to support the `link` function of the supported directives. * intended to support the `link` function of the supported directives.

View File

@ -31,6 +31,7 @@ define(
describe("The mct-include directive", function () { describe("The mct-include directive", function () {
var testTemplates, var testTemplates,
testUrls,
mockLinker, mockLinker,
mockScope, mockScope,
mockElement, mockElement,
@ -58,11 +59,21 @@ define(
templateUrl: "z/template.html" templateUrl: "z/template.html"
} }
]; ];
mockLinker = jasmine.createSpyObj('templateLinker', ['link']); testUrls = {};
testTemplates.forEach(function (t, i) {
testUrls[t.key] = "some URL " + String(i);
});
mockLinker = jasmine.createSpyObj(
'templateLinker',
['link', 'getPath']
);
mockScope = jasmine.createSpyObj('$scope', ['$watch', '$on']); mockScope = jasmine.createSpyObj('$scope', ['$watch', '$on']);
mockElement = jasmine.createSpyObj('element', ['empty']); mockElement = jasmine.createSpyObj('element', ['empty']);
mockChangeTemplate = jasmine.createSpy('changeTemplate'); mockChangeTemplate = jasmine.createSpy('changeTemplate');
mockLinker.link.andReturn(mockChangeTemplate); mockLinker.link.andReturn(mockChangeTemplate);
mockLinker.getPath.andCallFake(function (template) {
return testUrls[template.key];
});
mctInclude = new MCTInclude(testTemplates, mockLinker); mctInclude = new MCTInclude(testTemplates, mockLinker);
mctInclude.link(mockScope, mockElement, {}); mctInclude.link(mockScope, mockElement, {});
}); });
@ -80,15 +91,14 @@ define(
mockScope.key = 'abc'; mockScope.key = 'abc';
fireWatch('key', mockScope.key); fireWatch('key', mockScope.key);
expect(mockChangeTemplate) expect(mockChangeTemplate)
.toHaveBeenCalledWith("a/b/c/template.html"); .toHaveBeenCalledWith(testUrls.abc);
mockScope.key = 'xyz'; mockScope.key = 'xyz';
fireWatch('key', mockScope.key); fireWatch('key', mockScope.key);
expect(mockChangeTemplate) expect(mockChangeTemplate)
.toHaveBeenCalledWith("x/y/z/template.html"); .toHaveBeenCalledWith(testUrls.xyz);
}); });
}); });
} }
); );

View File

@ -36,6 +36,7 @@ define(
describe("The mct-representation directive", function () { describe("The mct-representation directive", function () {
var testRepresentations, var testRepresentations,
testViews, testViews,
testUrls,
mockRepresenters, mockRepresenters,
mockQ, mockQ,
mockLinker, mockLinker,
@ -64,6 +65,8 @@ define(
} }
beforeEach(function () { beforeEach(function () {
testUrls = {};
testRepresentations = [ testRepresentations = [
{ {
key: "abc", key: "abc",
@ -94,6 +97,11 @@ define(
testModel = { someKey: "some value" }; testModel = { someKey: "some value" };
testUrls = {};
testViews.concat(testRepresentations).forEach(function (t, i) {
testUrls[t.key] = "some URL " + String(i);
});
mockRepresenters = ["A", "B"].map(function (name) { mockRepresenters = ["A", "B"].map(function (name) {
var constructor = jasmine.createSpy("Representer" + name), var constructor = jasmine.createSpy("Representer" + name),
representer = jasmine.createSpyObj( representer = jasmine.createSpyObj(
@ -105,7 +113,10 @@ define(
}); });
mockQ = { when: mockPromise }; mockQ = { when: mockPromise };
mockLinker = jasmine.createSpyObj('templateLinker', ['link']); mockLinker = jasmine.createSpyObj(
'templateLinker',
['link', 'getPath']
);
mockChangeTemplate = jasmine.createSpy('changeTemplate'); mockChangeTemplate = jasmine.createSpy('changeTemplate');
mockLog = jasmine.createSpyObj("$log", LOG_FUNCTIONS); mockLog = jasmine.createSpyObj("$log", LOG_FUNCTIONS);
@ -115,6 +126,9 @@ define(
mockDomainObject.getModel.andReturn(testModel); mockDomainObject.getModel.andReturn(testModel);
mockLinker.link.andReturn(mockChangeTemplate); mockLinker.link.andReturn(mockChangeTemplate);
mockLinker.getPath.andCallFake(function (ext) {
return testUrls[ext.key];
});
mctRepresentation = new MCTRepresentation( mctRepresentation = new MCTRepresentation(
testRepresentations, testRepresentations,
@ -160,7 +174,7 @@ define(
fireWatch('domainObject', mockDomainObject); fireWatch('domainObject', mockDomainObject);
expect(mockChangeTemplate) expect(mockChangeTemplate)
.toHaveBeenCalledWith("a/b/c/template.html"); .toHaveBeenCalledWith(testUrls.abc);
}); });
it("recognizes keys for views", function () { it("recognizes keys for views", function () {
@ -172,7 +186,7 @@ define(
fireWatch('domainObject', mockDomainObject); fireWatch('domainObject', mockDomainObject);
expect(mockChangeTemplate) expect(mockChangeTemplate)
.toHaveBeenCalledWith("x/y/z/template.html"); .toHaveBeenCalledWith(testUrls.xyz);
}); });
it("does not load templates until there is an object", function () { it("does not load templates until there is an object", function () {

View File

@ -69,6 +69,16 @@ define(
); );
}); });
it("resolves extension paths", function () {
expect(linker.getPath({
bundle: {
path: 'a',
resources: 'b'
},
templateUrl: 'c/d.html'
})).toEqual('a/b/c/d.html');
});
describe("when linking elements", function () { describe("when linking elements", function () {
var changeTemplate, var changeTemplate,
commentElement; commentElement;