fix(dynamic_component_loader): implemented dispose for dynamically-loaded components

This commit is contained in:
vsavkin
2015-06-12 14:14:29 -07:00
parent 9613772455
commit 21dcfc89e9
4 changed files with 49 additions and 14 deletions

View File

@ -17,7 +17,7 @@ import {
import {TestBed, ViewProxy} from 'angular2/src/test_lib/test_bed';
import {Injector} from 'angular2/di';
import {Component, View} from 'angular2/annotations';
import {Component, View, onDestroy} from 'angular2/annotations';
import * as viewAnn from 'angular2/src/core/annotations_impl/view';
import {DynamicComponentLoader} from 'angular2/src/core/compiler/dynamic_component_loader';
import {ElementRef} from 'angular2/src/core/compiler/element_ref';
@ -68,6 +68,28 @@ export function main() {
});
}));
it('should allow destroying dynamically-loaded components',
inject([TestBed, AsyncTestCompleter], (tb, async) => {
tb.overrideView(MyComp, new viewAnn.View({
template: '<dynamic-comp #dynamic></dynamic-comp>',
directives: [DynamicComp]
}));
tb.createView(MyComp).then((view) => {
var dynamicComponent = view.rawView.locals.get("dynamic");
dynamicComponent.done.then((ref) => {
view.detectChanges();
expect(view.rootNodes).toHaveText("hello");
ref.dispose();
expect(ref.instance.destroyed).toBe(true);
expect(view.rootNodes).toHaveText("");
async.done();
});
});
}));
it('should allow to destroy and create them via viewcontainer directives',
ijTestBed((tb: TestBed, async) => {
tb.overrideView(MyComp, new viewAnn.View({
@ -287,16 +309,23 @@ class DynamicComp {
}
}
@Component({selector: 'hello-cmp', appInjector: [DynamicallyCreatedComponentService]})
@Component({
selector: 'hello-cmp',
appInjector: [DynamicallyCreatedComponentService],
lifecycle: [onDestroy]
})
@View({template: "{{greeting}}"})
class DynamicallyCreatedCmp {
greeting: string;
dynamicallyCreatedComponentService: DynamicallyCreatedComponentService;
destroyed: boolean = false;
constructor(a: DynamicallyCreatedComponentService) {
this.greeting = "hello";
this.dynamicallyCreatedComponentService = a;
}
onDestroy() { this.destroyed = true; }
}
@Component({selector: 'dummy'})