feat(changeDetection): detach ViewControllers when not active

This commit is contained in:
Adam Bradley
2016-04-17 00:17:07 -05:00
parent 2295bd9264
commit b282e90e2c
2 changed files with 24 additions and 1 deletions

View File

@@ -1410,6 +1410,9 @@ export class NavController extends Ion {
// set the ComponentRef's instance to this ViewController
view.setInstance(component);
// remember the ChangeDetectorRef for this ViewController
view.setChangeDetector(hostViewRef.changeDetectorRef);
// remember the ElementRef to the ion-page elementRef that was just created
view.setPageRef(pageElementRef);

View File

@@ -1,4 +1,4 @@
import {Output, EventEmitter, Type, TemplateRef, ViewContainerRef, ElementRef, Renderer} from 'angular2/core';
import {Output, EventEmitter, Type, TemplateRef, ViewContainerRef, ElementRef, Renderer, ChangeDetectorRef} from 'angular2/core';
import {Navbar} from '../navbar/navbar';
import {NavController, NavOptions} from './nav-controller';
@@ -33,6 +33,7 @@ export class ViewController {
private _nbVwRef: ViewContainerRef;
private _onDismiss: Function = null;
private _pgRef: ElementRef;
private _cd: ChangeDetectorRef;
protected _nav: NavController;
/**
@@ -163,6 +164,13 @@ export class ViewController {
return false;
}
/**
* @private
*/
setChangeDetector(cd: ChangeDetectorRef) {
this._cd = cd;
}
/**
* @private
*/
@@ -467,6 +475,14 @@ export class ViewController {
* The view is about to enter and become the active view.
*/
willEnter() {
if (this._cd) {
// ensure this has been re-attached to the change detector
this._cd.reattach();
// detect changes before we run any user code
this._cd.detectChanges();
}
ctrlFn(this, 'onPageWillEnter');
}
@@ -496,6 +512,10 @@ export class ViewController {
*/
didLeave() {
ctrlFn(this, 'onPageDidLeave');
// when this is not the active page
// we no longer need to detect changes
this._cd && this._cd.detach();
}
/**