|
1 | 1 | import {List, ListWrapper, MapWrapper} from 'angular2/src/facade/collection'; |
2 | | -import {IQueryList} from './interface_query'; |
3 | 2 |
|
4 | 3 | /** |
5 | | - * Injectable Objects that contains a live list of child directives in the light Dom of a directive. |
| 4 | + * An iterable and observable live list of components in the DOM. |
| 5 | + * |
| 6 | + * A QueryList contains a live list of child directives in the DOM of a directive. |
6 | 7 | * The directives are kept in depth-first pre-order traversal of the DOM. |
7 | 8 | * |
8 | | - * In the future this class will implement an Observable interface. |
9 | | - * For now it uses a plain list of observable callbacks. |
| 9 | + * The `QueryList` is iterable, therefore it can be used in both javascript code with `for..of` loop |
| 10 | + * as well as in template with `*ng-for="of"` directive. |
| 11 | + * |
| 12 | + * QueryList is updated as part of the change-detection cycle of a directive. Since change detection |
| 13 | + * happens after construction of a directive, QueryList will always be empty when observed in the |
| 14 | + * constructor. |
| 15 | + * |
| 16 | + * |
| 17 | + * NOTE: In the future this class will implement an `Observable` interface. For now it uses a plain |
| 18 | + * list of observable callbacks. |
| 19 | + * |
| 20 | + * # Example: |
| 21 | + * |
| 22 | + * Assume that `<tabs>` component would like to get a list its children which are `<pane>` |
| 23 | + * components as shown in this example: |
| 24 | + * |
| 25 | + * ```html |
| 26 | + * <tabs> |
| 27 | + * <pane title="Overview">...</pane> |
| 28 | + * <pane *ng-for="#o of objects" [title]="o.title">{{o.text}}</pane> |
| 29 | + * </tabs> |
| 30 | + * ``` |
| 31 | + * |
| 32 | + * In the above example the list of `<tabs>` elements needs to get a list of `<pane>` elements so |
| 33 | + * that it could render tabs with the correct titles and in the correct order. |
| 34 | + * |
| 35 | + * A possible solution would be for a `<pane>` to inject `<tabs>` component and then register itself |
| 36 | + * with `<tabs>` component's on `hydrate` and deregister on `dehydrate` event. While a reasonable |
| 37 | + * approach, this would only work partialy since `*ng-for` could rearrange the list of `<pane>` |
| 38 | + * components which would not be reported to `<tabs>` component and thus the list of `<pane>` |
| 39 | + * components would be out of sync with respect to the list of `<pane>` elements. |
| 40 | + * |
| 41 | + * A preferred solution is to inject a `QueryList` which is a live list of directives in the |
| 42 | + * component`s light DOM. |
| 43 | + * |
| 44 | + * ```javascript |
| 45 | + * @Component({ |
| 46 | + * selector: 'tabs' |
| 47 | + * }) |
| 48 | + * @View({ |
| 49 | + * template: ` |
| 50 | + * <ul> |
| 51 | + * <li *ng-for="#pane of panes">{{pane.title}}</li> |
| 52 | + * </ul> |
| 53 | + * <content></content> |
| 54 | + * ` |
| 55 | + * }) |
| 56 | + * class Tabs { |
| 57 | + * panes: QueryList<Pane> |
| 58 | + * |
| 59 | + * constructor(@Query(Pane) panes:QueryList<Pane>) { |
| 60 | + * this.panes = panes; |
| 61 | + * } |
| 62 | + * } |
| 63 | + * |
| 64 | + * @Component({ |
| 65 | + * selector: 'pane', |
| 66 | + * properties: ['title'] |
| 67 | + * }) |
| 68 | + * @View(...) |
| 69 | + * class Pane { |
| 70 | + * title:string; |
| 71 | + * } |
| 72 | + * ``` |
10 | 73 | */ |
11 | | -export class QueryList<T> implements IQueryList<T> { |
| 74 | +export class QueryList<T> { |
12 | 75 | protected _results: List < T >= []; |
13 | 76 | protected _callbacks: List < () => void >= []; |
14 | 77 | protected _dirty: boolean = false; |
|
0 commit comments