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.
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++) {
if (!Compiler._isValidDirective(directives[i])) {
@ -153,7 +153,7 @@ export class Compiler {
var boundDirectives =
ListWrapper.map(directives, (directive) => this._bindDirective(directive));
var renderTemplate = this._buildRenderTemplate(component, template, boundDirectives);
var renderTemplate = this._buildRenderTemplate(component, view, boundDirectives);
pvPromise =
this._render.compile(renderTemplate)
.then((renderPv) => {

View File

@ -10,9 +10,7 @@ export class MockViewResolver extends ViewResolver {
_viewCache: Map<Type, View> = new Map();
_directiveOverrides: Map<Type, Map<Type, Type>> = new Map();
constructor() {
super();
}
constructor() { super(); }
/**
* 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';
export class CompileStepFactory {
createSteps(template: ViewDefinition): List<CompileStep> { return null; }
createSteps(view: ViewDefinition): List<CompileStep> { return null; }
}
export class DefaultStepFactory extends CompileStepFactory {
constructor(public _parser: Parser, public _shadowDomStrategy: ShadowDomStrategy) { super(); }
createSteps(template: ViewDefinition): List<CompileStep> {
createSteps(view: ViewDefinition): List<CompileStep> {
return [
new ViewSplitter(this._parser),
new PropertyBindingParser(this._parser),
new DirectiveParser(this._parser, template.directives),
new DirectiveParser(this._parser, view.directives),
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';
/**
* Strategy to load component templates.
* Strategy to load component views.
* TODO: Make public API once we are more confident in this approach.
*/
@Injectable()

View File

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

View File

@ -74,7 +74,7 @@ export class TestComponentBuilder {
/**
* 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 {string} html
@ -130,9 +130,8 @@ export class TestComponentBuilder {
var mockViewResolver = this._injector.get(ViewResolver);
MapWrapper.forEach(this._viewOverrides,
(view, type) => { mockViewResolver.setView(type, view); });
MapWrapper.forEach(this._templateOverrides, (template, type) => {
mockViewResolver.setInlineTemplate(type, template);
});
MapWrapper.forEach(this._templateOverrides,
(template, type) => { mockViewResolver.setInlineTemplate(type, template); });
MapWrapper.forEach(this._directiveOverrides, (overrides, component) => {
MapWrapper.forEach(overrides, (to, from) => {
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;
}
setView(component: Type, template: viewAnn.View): void {
this._cmpViews.set(component, template);
}
setView(component: Type, view: viewAnn.View): void { this._cmpViews.set(component, view); }
}
class FakeProtoViewFactory extends ProtoViewFactory {

View File

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