X Tutup
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions modules/angular2/src/core/compiler/element_injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ export class ElementInjector extends TreeNode<ElementInjector> implements Depend
this._preBuiltObjects = null;
this._strategy.callOnDestroy();
this._strategy.dehydrate();
this._clearQueryLists();
}

afterContentChecked(): void {
Expand Down Expand Up @@ -837,6 +838,12 @@ export class ElementInjector extends TreeNode<ElementInjector> implements Depend
var nestedView = view.getNestedView(view.elementOffset + this.getBoundElementIndex());
return isPresent(nestedView) ? nestedView.rootElementInjectors : [];
}

private _clearQueryLists(): void {
if (isPresent(this._query0) && this._query0.originator === this) this._query0.reset();
if (isPresent(this._query1) && this._query1.originator === this) this._query1.reset();
if (isPresent(this._query2) && this._query2.originator === this) this._query2.reset();
}
}

interface _ElementInjectorStrategy {
Expand Down Expand Up @@ -1163,4 +1170,9 @@ export class QueryRef {
private _aggregateDirective(inj: ElementInjector, aggregator: any[]): void {
inj.addDirectivesMatchingQuery(this.query, aggregator);
}

reset(): void {
this.list.reset([]);
this.list.removeAllCallbacks();
}
}
19 changes: 12 additions & 7 deletions modules/angular2/src/core/compiler/query_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,6 @@ class QueryList<T> extends Object
_dirty = true;
}

// TODO(rado): hook up with change detection after #995.
void fireCallbacks() {
if (_dirty) {
_callbacks.forEach((c) => c());
_dirty = false;
}
}

void onChange(callback) {
_callbacks.add(callback);
Expand All @@ -105,6 +98,10 @@ class QueryList<T> extends Object
_callbacks.remove(callback);
}

void removeAllCallbacks() {
this._callbacks = [];
}

int get length => _results.length;
T get first => _results.first;
T get last => _results.last;
Expand All @@ -116,4 +113,12 @@ class QueryList<T> extends Object
// Note: we need to return a list instead of iterable to match JS.
return this._results.map(fn).toList();
}

// Internal to the framework.
void fireCallbacks() {
if (_dirty) {
_callbacks.forEach((c) => c());
_dirty = false;
}
}
}
16 changes: 10 additions & 6 deletions modules/angular2/src/core/compiler/query_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,13 @@ export class QueryList<T> {
this._dirty = true;
}

fireCallbacks(): void {
if (this._dirty) {
ListWrapper.forEach(this._callbacks, (c) => c());
this._dirty = false;
}
}

onChange(callback: () => void): void { this._callbacks.push(callback); }

removeCallback(callback: () => void): void { ListWrapper.remove(this._callbacks, callback); }

removeAllCallbacks(): void { this._callbacks = []; }

toString(): string { return this._results.toString(); }

get length(): number { return this._results.length; }
Expand All @@ -106,4 +102,12 @@ export class QueryList<T> {
map<U>(fn: (item: T) => U): U[] { return this._results.map(fn); }

[Symbol.iterator](): any { return this._results[Symbol.iterator](); }

// Internal to the framework.
fireCallbacks(): void {
if (this._dirty) {
ListWrapper.forEach(this._callbacks, (c) => c());
this._dirty = false;
}
}
}
28 changes: 28 additions & 0 deletions modules/angular2/test/core/compiler/query_integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,34 @@ export function main() {
view.detectChanges();
});
}));

it('should correctly clean-up when destroyed together with the directives it is querying',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template =
'<needs-query #q *ng-if="shouldShow"><div text="foo"></div></needs-query>';

tcb.overrideTemplate(MyComp, template)
.createAsync(MyComp)
.then((view) => {
view.componentInstance.shouldShow = true;
view.detectChanges();

var q: NeedsQuery = view.componentViewChildren[1].getLocal('q');
expect(q.query.length).toEqual(1);

view.componentInstance.shouldShow = false;
view.detectChanges();

view.componentInstance.shouldShow = true;
view.detectChanges();

var q2: NeedsQuery = view.componentViewChildren[1].getLocal('q');

expect(q2.query.length).toEqual(1);

async.done();
});
}));
});

describe("querying by var binding", () => {
Expand Down
13 changes: 13 additions & 0 deletions modules/angular2/test/core/compiler/query_list_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ export function main() {
queryList.fireCallbacks();
expect(fires).toEqual(1);
});

it('should support removing all callbacks', () => {
var fires = 0;
var callback = () => fires += 1;
queryList.onChange(callback);

queryList.add('one');
queryList.removeAllCallbacks();

queryList.fireCallbacks();

expect(fires).toEqual(0);
});
});
});
}
X Tutup