FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(detached-loader): completely deatch components by edusperoni · Pull Request #2257 · NativeScript/nativescript-angular · GitHub

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
19 changes: 15 additions & 4 deletions nativescript-angular/common/detached-loader.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentRef, ComponentFactory, ViewContainerRef, Component, Type, ComponentFactoryResolver, ChangeDetectorRef } from '@angular/core';
import { ComponentRef, ComponentFactory, ViewContainerRef, Component, Type, ComponentFactoryResolver, ChangeDetectorRef, ApplicationRef, OnDestroy } from '@angular/core';
import { Trace } from '@nativescript/core';

/**
Expand All @@ -10,13 +10,20 @@ import { Trace } from '@nativescript/core';
selector: 'DetachedContainer',
template: `<Placeholder #loader></Placeholder>`,
})
export class DetachedLoader {
export class DetachedLoader implements OnDestroy {
private disposeFunctions: Array<() => void> = [];
// tslint:disable-line:component-class-suffix
constructor(private resolver: ComponentFactoryResolver, private changeDetector: ChangeDetectorRef, private containerRef: ViewContainerRef) {}
constructor(private resolver: ComponentFactoryResolver, private changeDetector: ChangeDetectorRef, private containerRef: ViewContainerRef, private appRef: ApplicationRef) {}

private loadInLocation(componentType: Type<any>): Promise<ComponentRef<any>> {
const factory = this.resolver.resolveComponentFactory(componentType);
const componentRef = this.containerRef.createComponent(factory, this.containerRef.length, this.containerRef.injector);
const componentRef = factory.create(this.containerRef.injector);
this.appRef.attachView(componentRef.hostView);

this.disposeFunctions.push(() => {
this.appRef.detachView(componentRef.hostView);
componentRef.destroy();
});

// Component is created, built may not be checked if we are loading
// inside component with OnPush CD strategy. Mark us for check to be sure CD will reach us.
Expand All @@ -27,6 +34,10 @@ export class DetachedLoader {
return Promise.resolve(componentRef);
}

public ngOnDestroy() {
this.disposeFunctions.forEach((fn) => fn());
}

public detectChanges() {
this.changeDetector.markForCheck();
}
Expand Down

Back | FazBrowse Home | New Git URL