fix(query): view query should not be updated when subviews are attached.

This commit is contained in:
Rado Kirov 2015-07-27 17:04:04 -07:00
parent c1ee943533
commit 34acef58e7
2 changed files with 52 additions and 2 deletions

View File

@ -789,8 +789,10 @@ export class ElementInjector extends TreeNode<ElementInjector> implements Depend
}
this.remove();
ListWrapper.forEach(queriesToUpdate, (q) => q.update());
// TODO(rado): update should work on view queries too, however currently it
// is not implemented, so we filter to non-view queries.
var nonViewQueries = ListWrapper.filter(queriesToUpdate, (q) => !q.query.isViewQuery);
ListWrapper.forEach(nonViewQueries, (q) => q.update());
}
private _pruneQueryFromTree(query: QueryRef): void {

View File

@ -365,6 +365,30 @@ export function main() {
});
}));
it('should not be affected by other changes in the component',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template = '<needs-view-query-nested-if #q></needs-view-query-nested-if>';
tcb.overrideTemplate(MyComp, template)
.createAsync(MyComp)
.then((view) => {
var q: NeedsViewQueryNestedIf = view.componentViewChildren[0].getLocal("q");
view.detectChanges();
expect(q.query.length).toEqual(1);
expect(q.query.first.text).toEqual("1");
q.show = false;
view.detectChanges();
expect(q.query.length).toEqual(1);
expect(q.query.first.text).toEqual("1");
async.done();
});
}));
/* TODO(rado): fix and reenable.
it('should maintain directives in pre-order depth-first DOM order after dynamic insertion',
@ -396,6 +420,12 @@ class TextDirective {
constructor() {}
}
@Directive({selector: '[dir]'})
@Injectable()
class InertDirective {
constructor() {}
}
@Component({selector: 'needs-query'})
@View({
directives: [NgFor, TextDirective],
@ -487,6 +517,22 @@ class NeedsViewQueryIf {
}
@Component({selector: 'needs-view-query-nested-if'})
@View({
directives: [NgIf, InertDirective, TextDirective],
template: '<div text="1"><div *ng-if="show"><div dir></div></div></div>'
})
@Injectable()
class NeedsViewQueryNestedIf {
show: boolean;
query: QueryList<TextDirective>;
constructor(@ViewQuery(TextDirective) query: QueryList<TextDirective>) {
this.query = query;
this.show = true;
}
}
@Component({selector: 'needs-view-query-order'})
@View({
directives: [NgFor, TextDirective],
@ -511,8 +557,10 @@ class NeedsViewQueryOrder {
NeedsViewQuery,
NeedsViewQueryDesc,
NeedsViewQueryIf,
NeedsViewQueryNestedIf,
NeedsViewQueryOrder,
TextDirective,
InertDirective,
NgIf,
NgFor
]