Skip to content

Commit

Permalink
feat(changeDetection): detach ViewControllers when not active
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Apr 17, 2016
1 parent 2295bd9 commit b282e90
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ionic/components/nav/nav-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
22 changes: 21 additions & 1 deletion ionic/components/nav/view-controller.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -33,6 +33,7 @@ export class ViewController {
private _nbVwRef: ViewContainerRef;
private _onDismiss: Function = null;
private _pgRef: ElementRef;
private _cd: ChangeDetectorRef;
protected _nav: NavController;

/**
Expand Down Expand Up @@ -163,6 +164,13 @@ export class ViewController {
return false;
}

/**
* @private
*/
setChangeDetector(cd: ChangeDetectorRef) {
this._cd = cd;
}

/**
* @private
*/
Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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();
}

/**
Expand Down

0 comments on commit b282e90

Please sign in to comment.