refactor: change template for view where it makes sense

This commit is contained in:
Victor Berchet 2015-06-24 11:10:29 +02:00
parent b15474c6f2
commit a0e0f3123b
8 changed files with 52 additions and 58 deletions

View File

@ -139,9 +139,9 @@ export class Compiler {
// It happens when a template references a component multiple times. // It happens when a template references a component multiple times.
return pvPromise; return pvPromise;
} }
var template = this._viewResolver.resolve(component); var view = this._viewResolver.resolve(component);
var directives = this._flattenDirectives(template); var directives = this._flattenDirectives(view);
for (var i = 0; i < directives.length; i++) { for (var i = 0; i < directives.length; i++) {
if (!Compiler._isValidDirective(directives[i])) { if (!Compiler._isValidDirective(directives[i])) {
@ -153,7 +153,7 @@ export class Compiler {
var boundDirectives = var boundDirectives =
ListWrapper.map(directives, (directive) => this._bindDirective(directive)); ListWrapper.map(directives, (directive) => this._bindDirective(directive));
var renderTemplate = this._buildRenderTemplate(component, template, boundDirectives); var renderTemplate = this._buildRenderTemplate(component, view, boundDirectives);
pvPromise = pvPromise =
this._render.compile(renderTemplate) this._render.compile(renderTemplate)
.then((renderPv) => { .then((renderPv) => {

View File

@ -10,9 +10,7 @@ export class MockViewResolver extends ViewResolver {
_viewCache: Map<Type, View> = new Map(); _viewCache: Map<Type, View> = new Map();
_directiveOverrides: Map<Type, Map<Type, Type>> = new Map(); _directiveOverrides: Map<Type, Map<Type, Type>> = new Map();
constructor() { constructor() { super(); }
super();
}
/** /**
* Overrides the {@link View} for a component. * Overrides the {@link View} for a component.

View File

@ -10,19 +10,19 @@ import {ShadowDomCompileStep} from '../shadow_dom/shadow_dom_compile_step';
import {ShadowDomStrategy} from '../shadow_dom/shadow_dom_strategy'; import {ShadowDomStrategy} from '../shadow_dom/shadow_dom_strategy';
export class CompileStepFactory { export class CompileStepFactory {
createSteps(template: ViewDefinition): List<CompileStep> { return null; } createSteps(view: ViewDefinition): List<CompileStep> { return null; }
} }
export class DefaultStepFactory extends CompileStepFactory { export class DefaultStepFactory extends CompileStepFactory {
constructor(public _parser: Parser, public _shadowDomStrategy: ShadowDomStrategy) { super(); } constructor(public _parser: Parser, public _shadowDomStrategy: ShadowDomStrategy) { super(); }
createSteps(template: ViewDefinition): List<CompileStep> { createSteps(view: ViewDefinition): List<CompileStep> {
return [ return [
new ViewSplitter(this._parser), new ViewSplitter(this._parser),
new PropertyBindingParser(this._parser), new PropertyBindingParser(this._parser),
new DirectiveParser(this._parser, template.directives), new DirectiveParser(this._parser, view.directives),
new TextInterpolationParser(this._parser), new TextInterpolationParser(this._parser),
new ShadowDomCompileStep(this._shadowDomStrategy, template) new ShadowDomCompileStep(this._shadowDomStrategy, view)
]; ];
} }
} }

View File

@ -12,7 +12,7 @@ import {StyleInliner} from './style_inliner';
import {StyleUrlResolver} from './style_url_resolver'; import {StyleUrlResolver} from './style_url_resolver';
/** /**
* Strategy to load component templates. * Strategy to load component views.
* TODO: Make public API once we are more confident in this approach. * TODO: Make public API once we are more confident in this approach.
*/ */
@Injectable() @Injectable()

View File

@ -37,10 +37,10 @@ export class TestBed {
* @see setInlineTemplate() to only override the html * @see setInlineTemplate() to only override the html
* *
* @param {Type} component * @param {Type} component
* @param {ViewDefinition} template * @param {ViewDefinition} view
*/ */
overrideView(component: Type, template: View): void { overrideView(component: Type, view: View): void {
this._injector.get(ViewResolver).setView(component, template); this._injector.get(ViewResolver).setView(component, view);
} }
/** /**

View File

@ -74,7 +74,7 @@ export class TestComponentBuilder {
/** /**
* Overrides only the html of a {@link Component}. * Overrides only the html of a {@link Component}.
* All the other propoerties of the component's {@link View} are preserved. * All the other properties of the component's {@link View} are preserved.
* *
* @param {Type} component * @param {Type} component
* @param {string} html * @param {string} html
@ -130,9 +130,8 @@ export class TestComponentBuilder {
var mockViewResolver = this._injector.get(ViewResolver); var mockViewResolver = this._injector.get(ViewResolver);
MapWrapper.forEach(this._viewOverrides, MapWrapper.forEach(this._viewOverrides,
(view, type) => { mockViewResolver.setView(type, view); }); (view, type) => { mockViewResolver.setView(type, view); });
MapWrapper.forEach(this._templateOverrides, (template, type) => { MapWrapper.forEach(this._templateOverrides,
mockViewResolver.setInlineTemplate(type, template); (template, type) => { mockViewResolver.setInlineTemplate(type, template); });
});
MapWrapper.forEach(this._directiveOverrides, (overrides, component) => { MapWrapper.forEach(this._directiveOverrides, (overrides, component) => {
MapWrapper.forEach(overrides, (to, from) => { MapWrapper.forEach(overrides, (to, from) => {
mockViewResolver.overrideViewDirective(component, from, to); mockViewResolver.overrideViewDirective(component, from, to);

View File

@ -577,9 +577,7 @@ class FakeViewResolver extends ViewResolver {
return this._cmpViews.has(component) ? this._cmpViews.get(component) : null; return this._cmpViews.has(component) ? this._cmpViews.get(component) : null;
} }
setView(component: Type, template: viewAnn.View): void { setView(component: Type, view: viewAnn.View): void { this._cmpViews.set(component, view); }
this._cmpViews.set(component, template);
}
} }
class FakeProtoViewFactory extends ProtoViewFactory { class FakeProtoViewFactory extends ProtoViewFactory {

View File

@ -20,30 +20,29 @@ import * as viewImpl from 'angular2/src/core/annotations_impl/view';
export function main() { export function main() {
describe('MockViewResolver', () => { describe('MockViewResolver', () => {
var resolver; var viewResolver;
beforeEach(() => { resolver = new MockViewResolver(); }); beforeEach(() => { viewResolver = new MockViewResolver(); });
describe('View overriding', () => { describe('View overriding', () => {
it('should fallback to the default ViewResolver when templates are not overridden', it('should fallback to the default ViewResolver when templates are not overridden', () => {
() => { var view = viewResolver.resolve(SomeComponent);
var template = resolver.resolve(SomeComponent); expect(view.template).toEqual('template');
expect(template.template).toEqual('template'); expect(view.directives).toEqual([SomeDirective]);
expect(template.directives).toEqual([SomeDirective]);
}); });
it('should allow overriding the @View', () => { it('should allow overriding the @View', () => {
resolver.setView(SomeComponent, new viewImpl.View({template: 'overridden template'})); viewResolver.setView(SomeComponent, new viewImpl.View({template: 'overridden template'}));
var template = resolver.resolve(SomeComponent); var view = viewResolver.resolve(SomeComponent);
expect(template.template).toEqual('overridden template'); expect(view.template).toEqual('overridden template');
expect(isBlank(template.directives)).toBe(true); expect(isBlank(view.directives)).toBe(true);
}); });
it('should not allow overriding a view after it has been resolved', () => { it('should not allow overriding a view after it has been resolved', () => {
resolver.resolve(SomeComponent); viewResolver.resolve(SomeComponent);
expect(() => { expect(() => {
resolver.setView(SomeComponent, new viewImpl.View({template: 'overridden template'})); viewResolver.setView(SomeComponent, new viewImpl.View({template: 'overridden template'}));
}) })
.toThrowError( .toThrowError(
`The component ${stringify(SomeComponent)} has already been compiled, its configuration can not be changed`); `The component ${stringify(SomeComponent)} has already been compiled, its configuration can not be changed`);
@ -52,22 +51,22 @@ export function main() {
describe('inline template definition overriding', () => { describe('inline template definition overriding', () => {
it('should allow overriding the default template', () => { it('should allow overriding the default template', () => {
resolver.setInlineTemplate(SomeComponent, 'overridden template'); viewResolver.setInlineTemplate(SomeComponent, 'overridden template');
var template = resolver.resolve(SomeComponent); var view = viewResolver.resolve(SomeComponent);
expect(template.template).toEqual('overridden template'); expect(view.template).toEqual('overridden template');
expect(template.directives).toEqual([SomeDirective]); expect(view.directives).toEqual([SomeDirective]);
}); });
it('should allow overriding an overriden @View', () => { it('should allow overriding an overriden @View', () => {
resolver.setView(SomeComponent, new viewImpl.View({template: 'overridden template'})); viewResolver.setView(SomeComponent, new viewImpl.View({template: 'overridden template'}));
resolver.setInlineTemplate(SomeComponent, 'overridden template x 2'); viewResolver.setInlineTemplate(SomeComponent, 'overridden template x 2');
var template = resolver.resolve(SomeComponent); var view = viewResolver.resolve(SomeComponent);
expect(template.template).toEqual('overridden template x 2'); expect(view.template).toEqual('overridden template x 2');
}); });
it('should not allow overriding a view after it has been resolved', () => { it('should not allow overriding a view after it has been resolved', () => {
resolver.resolve(SomeComponent); viewResolver.resolve(SomeComponent);
expect(() => { resolver.setInlineTemplate(SomeComponent, 'overridden template'); }) expect(() => { viewResolver.setInlineTemplate(SomeComponent, 'overridden template'); })
.toThrowError( .toThrowError(
`The component ${stringify(SomeComponent)} has already been compiled, its configuration can not be changed`); `The component ${stringify(SomeComponent)} has already been compiled, its configuration can not be changed`);
}); });
@ -76,31 +75,31 @@ export function main() {
describe('Directive overriding', () => { describe('Directive overriding', () => {
it('should allow overriding a directive from the default view', () => { it('should allow overriding a directive from the default view', () => {
resolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective); viewResolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective);
var template = resolver.resolve(SomeComponent); var view = viewResolver.resolve(SomeComponent);
expect(template.directives.length).toEqual(1); expect(view.directives.length).toEqual(1);
expect(template.directives[0]).toBe(SomeOtherDirective); expect(view.directives[0]).toBe(SomeOtherDirective);
}); });
it('should allow overriding a directive from an overriden @View', () => { it('should allow overriding a directive from an overriden @View', () => {
resolver.setView(SomeComponent, new viewImpl.View({directives: [SomeOtherDirective]})); viewResolver.setView(SomeComponent, new viewImpl.View({directives: [SomeOtherDirective]}));
resolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeComponent); viewResolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeComponent);
var template = resolver.resolve(SomeComponent); var view = viewResolver.resolve(SomeComponent);
expect(template.directives.length).toEqual(1); expect(view.directives.length).toEqual(1);
expect(template.directives[0]).toBe(SomeComponent); expect(view.directives[0]).toBe(SomeComponent);
}); });
it('should throw when the overridden directive is not present', () => { it('should throw when the overridden directive is not present', () => {
resolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeDirective); viewResolver.overrideViewDirective(SomeComponent, SomeOtherDirective, SomeDirective);
expect(() => { resolver.resolve(SomeComponent); }) expect(() => { viewResolver.resolve(SomeComponent); })
.toThrowError( .toThrowError(
`Overriden directive ${stringify(SomeOtherDirective)} not found in the template of ${stringify(SomeComponent)}`); `Overriden directive ${stringify(SomeOtherDirective)} not found in the template of ${stringify(SomeComponent)}`);
}); });
it('should not allow overriding a directive after its view has been resolved', () => { it('should not allow overriding a directive after its view has been resolved', () => {
resolver.resolve(SomeComponent); viewResolver.resolve(SomeComponent);
expect(() => { expect(() => {
resolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective); viewResolver.overrideViewDirective(SomeComponent, SomeDirective, SomeOtherDirective);
}) })
.toThrowError( .toThrowError(
`The component ${stringify(SomeComponent)} has already been compiled, its configuration can not be changed`); `The component ${stringify(SomeComponent)} has already been compiled, its configuration can not be changed`);