Skip to content

Commit 355ab5b

Browse files
committed
feat(query): adds support for descendants and more list apis.
Additional clean up of query code. Closes: angular#1935 BREAKING CHANGE: By default Query only queries direct children.
1 parent ca09701 commit 355ab5b

File tree

8 files changed

+150
-116
lines changed

8 files changed

+150
-116
lines changed

modules/angular2/src/core/annotations_impl/annotations.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,13 @@ import {DEFAULT} from 'angular2/change_detection';
231231
*
232232
* ### Injecting a live collection of descendant directives
233233
*
234-
* Note: This is will be implemented in later release. ()
235-
*
236-
* Similar to `@Query` above, but also includes the children of the child elements.
234+
* By passing the descendant flag to `@Query` above, we can include the children of the child
235+
* elements.
237236
*
238237
* ```
239238
* @Directive({ selector: '[my-directive]' })
240239
* class MyDirective {
241-
* constructor(@QueryDescendents(Dependency) dependencies:QueryList<Dependency>) {
240+
* constructor(@Query(Dependency, {descendants: true}) dependencies:QueryList<Dependency>) {
242241
* }
243242
* }
244243
* ```

modules/angular2/src/core/annotations_impl/di.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import {CONST, stringify} from 'angular2/src/facade/lang';
1+
import {CONST, stringify, isPresent} from 'angular2/src/facade/lang';
22
import {DependencyAnnotation} from 'angular2/src/di/annotations_impl';
3+
import {resolveForwardRef} from 'angular2/di';
34

45
/**
56
* Specifies that a constant attribute value should be injected.
@@ -53,6 +54,13 @@ export class Attribute extends DependencyAnnotation {
5354
*/
5455
@CONST()
5556
export class Query extends DependencyAnnotation {
56-
constructor(public directive: any) { super(); }
57+
descendants: boolean;
58+
constructor(private _directive: any, {descendants = false}: {descendants?: boolean} = {}) {
59+
super();
60+
this.descendants = descendants;
61+
}
62+
63+
get directive() { return resolveForwardRef(this._directive); }
64+
5765
toString() { return `@Query(${stringify(this.directive)})`; }
5866
}

modules/angular2/src/core/compiler/base_query_list.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@ class BaseQueryList<T> extends Object with IterableMixin<T> {
4646
removeCallback(callback) {
4747
this._callbacks.remove(callback);
4848
}
49+
50+
get length => this._results.length;
51+
get first => this._results.first;
52+
get last => this._results.last;
4953
}

modules/angular2/src/core/compiler/base_query_list.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {List, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
1+
import {List, ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
22

33
/**
44
* Injectable Objects that contains a live list of child directives in the light Dom of a directive.
@@ -10,9 +10,9 @@ import {List, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
1010
* @exportedAs angular2/view
1111
*/
1212
export class BaseQueryList<T> {
13-
_results: List<T>;
14-
_callbacks;
15-
_dirty;
13+
protected _results: List<T>;
14+
protected _callbacks;
15+
protected _dirty;
1616

1717
constructor() {
1818
this._results = [];
@@ -43,4 +43,10 @@ export class BaseQueryList<T> {
4343
onChange(callback) { ListWrapper.push(this._callbacks, callback); }
4444

4545
removeCallback(callback) { ListWrapper.remove(this._callbacks, callback); }
46+
47+
get length() { return this._results.length; }
48+
49+
get first() { return ListWrapper.first(this._results); }
50+
51+
get last() { return ListWrapper.last(this._results); }
4652
}

0 commit comments

Comments
 (0)