feat(core): add support for ContentChildren and ViewChildren

This commit is contained in:
vsavkin
2015-09-17 18:45:14 -07:00
committed by Victor Savkin
parent 5809a02624
commit 5dbe292615
9 changed files with 348 additions and 32 deletions

View File

@ -201,6 +201,34 @@ export class QueryMetadata extends DependencyMetadata {
toString(): string { return `@Query(${stringify(this.selector)})`; }
}
// TODO: add an example after ContentChildren and ViewChildren are in master
/**
* Configures a content query.
*
* Content queries are set before the `afterContentInit` callback is called.
*
* ### Example
*
* ```
* @Directive({
* selector: 'someDir'
* })
* class SomeDir {
* @ContentChildren(ChildDirective) contentChildren: QueryList<ChildDirective>;
*
* afterContentInit() {
* // contentChildren is set
* }
* }
* ```
*/
@CONST()
export class ContentChildrenMetadata extends QueryMetadata {
constructor(_selector: Type | string, {descendants = false}: {descendants?: boolean} = {}) {
super(_selector, {descendants: descendants});
}
}
/**
* Similar to {@link QueryMetadata}, but querying the component view, instead of
* the content children.
@ -248,3 +276,29 @@ export class ViewQueryMetadata extends QueryMetadata {
get isViewQuery() { return true; }
toString(): string { return `@ViewQuery(${stringify(this.selector)})`; }
}
/**
* Configures a view query.
*
* View queries are set before the `afterViewInit` callback is called.
*
* ### Example
*
* ```
* @Component({
* selector: 'someDir'
* })
* @View({templateUrl: 'someTemplate', directives: [ItemDirective]})
* class SomeDir {
* @ViewChildren(ItemDirective) viewChildren: QueryList<ItemDirective>;
*
* afterViewInit() {
* // viewChildren is set
* }
* }
* ```
*/
@CONST()
export class ViewChildrenMetadata extends ViewQueryMetadata {
constructor(_selector: Type | string) { super(_selector, {descendants: true}); }
}

View File

@ -719,8 +719,45 @@ export class DirectiveMetadata extends InjectableMetadata {
*/
moduleId: string;
// TODO: add an example after ContentChildren and ViewChildren are in master
/**
* Configures the queries that will be injected into the directive.
*
* Content queries are set before the `afterContentInit` callback is called.
* View queries are set before the `afterViewInit` callback is called.
*
* ### Example
*
* ```
* @Component({
* selector: 'someDir',
* queries: {
* contentChildren: new ContentChildren(ChildDirective),
* viewChildren: new ViewChildren(ChildDirective)
* }
* })
* @View({
* template: '<child-directive></child-directive>',
* directives: [ChildDirective]
* })
* class SomeDir {
* contentChildren: QueryList<ChildDirective>,
* viewChildren: QueryList<ChildDirective>
*
* afterContentInit() {
* // contentChildren is set
* }
*
* afterViewInit() {
* // viewChildren is set
* }
* }
* ```
*/
queries: StringMap<string, any>;
constructor({
selector, properties, events, host, bindings, exportAs, moduleId,
selector, properties, events, host, bindings, exportAs, moduleId, queries,
compileChildren = true,
}: {
selector?: string,
@ -730,6 +767,7 @@ export class DirectiveMetadata extends InjectableMetadata {
bindings?: any[],
exportAs?: string,
moduleId?: string,
queries?: StringMap<string, any>,
compileChildren?: boolean,
} = {}) {
super();
@ -739,6 +777,7 @@ export class DirectiveMetadata extends InjectableMetadata {
this.host = host;
this.exportAs = exportAs;
this.moduleId = moduleId;
this.queries = queries;
this.compileChildren = compileChildren;
this.bindings = bindings;
}
@ -862,7 +901,7 @@ export class ComponentMetadata extends DirectiveMetadata {
viewBindings: any[];
constructor({selector, properties, events, host, dynamicLoadable, exportAs, moduleId, bindings,
viewBindings, changeDetection = ChangeDetectionStrategy.Default,
queries, viewBindings, changeDetection = ChangeDetectionStrategy.Default,
compileChildren = true}: {
selector?: string,
properties?: string[],
@ -874,6 +913,7 @@ export class ComponentMetadata extends DirectiveMetadata {
moduleId?: string,
compileChildren?: boolean,
viewBindings?: any[],
queries?: StringMap<string, any>,
changeDetection?: ChangeDetectionStrategy,
} = {}) {
super({
@ -884,6 +924,7 @@ export class ComponentMetadata extends DirectiveMetadata {
exportAs: exportAs,
moduleId: moduleId,
bindings: bindings,
queries: queries,
compileChildren: compileChildren
});