[Representation] Update compilation approach

Update compilation approach for templateLinker to more
closely resemble ng-include; minimizes likelihood of
subtle behavioral differences (e.g. incorrect size
selection for split pane)
This commit is contained in:
Victor Woeltjen
2015-10-30 16:37:47 -07:00
parent 454a63e1f1
commit ae928a138c
2 changed files with 24 additions and 9 deletions

View File

@ -121,8 +121,8 @@ define(
function populateElement(template) { function populateElement(template) {
destroyScope(); destroyScope();
activeScope = scope.$new(false); activeScope = scope.$new(false);
element.empty(); element.html(template);
element.append(self.$compile(template)(activeScope)); self.$compile(element.contents())(activeScope);
} }
function badTemplate(templateUrl) { function badTemplate(templateUrl) {

View File

@ -27,7 +27,7 @@ define(
function (TemplateLinker) { function (TemplateLinker) {
'use strict'; 'use strict';
var JQLITE_METHODS = [ 'replaceWith', 'empty', 'append' ], var JQLITE_METHODS = [ 'replaceWith', 'empty', 'html', 'contents' ],
SCOPE_METHODS = [ '$on', '$new', '$destroy' ]; SCOPE_METHODS = [ '$on', '$new', '$destroy' ];
describe("TemplateLinker", function () { describe("TemplateLinker", function () {
@ -39,6 +39,7 @@ define(
mockElement, mockElement,
mockTemplates, mockTemplates,
mockElements, mockElements,
mockContents,
mockNewScope, mockNewScope,
mockPromise, mockPromise,
linker; linker;
@ -54,9 +55,12 @@ define(
mockPromise = jasmine.createSpyObj('promise', ['then']); mockPromise = jasmine.createSpyObj('promise', ['then']);
mockTemplates = {}; mockTemplates = {};
mockElements = {}; mockElements = {};
mockContents = {};
mockTemplateRequest.andReturn(mockPromise); mockTemplateRequest.andReturn(mockPromise);
mockCompile.andCallFake(function (html) { mockCompile.andCallFake(function (toCompile) {
var html = typeof toCompile === 'string' ?
toCompile : toCompile.testHtml;
mockTemplates[html] = jasmine.createSpy('template'); mockTemplates[html] = jasmine.createSpy('template');
mockElements[html] = mockElements[html] =
jasmine.createSpyObj('templateEl', JQLITE_METHODS); jasmine.createSpyObj('templateEl', JQLITE_METHODS);
@ -67,6 +71,16 @@ define(
return { trusted: url }; return { trusted: url };
}); });
mockScope.$new.andReturn(mockNewScope); mockScope.$new.andReturn(mockNewScope);
mockElement.html.andCallFake(function (html) {
mockContents[html] =
jasmine.createSpyObj('contentsEl', JQLITE_METHODS);
mockContents[html].testHtml = html;
});
mockElement.contents.andCallFake(function () {
return mockContents[
mockElement.html.mostRecentCall.args[0]
];
});
linker = new TemplateLinker( linker = new TemplateLinker(
mockTemplateRequest, mockTemplateRequest,
@ -135,8 +149,9 @@ define(
}, false); }, false);
}); });
it("compiles loaded templates with a new scope", function () { it("compiles element contents with a new scope", function () {
expect(mockCompile).toHaveBeenCalledWith(testTemplate); expect(mockCompile)
.toHaveBeenCalledWith(mockContents[testTemplate]);
expect(mockTemplates[testTemplate]) expect(mockTemplates[testTemplate])
.toHaveBeenCalledWith(mockNewScope); .toHaveBeenCalledWith(mockNewScope);
}); });
@ -146,9 +161,9 @@ define(
.toHaveBeenCalledWith(mockElement); .toHaveBeenCalledWith(mockElement);
}); });
it("appends rendered content to the specified element", function () { it("inserts HTML content into the specified element", function () {
expect(mockElement.append) expect(mockElement.html)
.toHaveBeenCalledWith(mockElements[testTemplate]); .toHaveBeenCalledWith(testTemplate);
}); });
it("clears templates when called with undefined", function () { it("clears templates when called with undefined", function () {