Skip to content

Commit

Permalink
fix(change_detection): ChangeDetectorRef reattach should restore orig…
Browse files Browse the repository at this point in the history
…inal mode

After using ChangeDetectorRef detach, it should keep the ChangeDetector mode so that it is restored after calling reattach.

closes #7078
closes #7080
  • Loading branch information
alfonso-presa authored and tbosch committed Jun 17, 2016
1 parent 791153c commit 773c349
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
10 changes: 8 additions & 2 deletions modules/@angular/core/src/linker/view_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ export abstract class EmbeddedViewRef<C> extends ViewRef {
}

export class ViewRef_<C> implements EmbeddedViewRef<C>, ChangeDetectorRef {
constructor(private _view: AppView<C>) { this._view = _view; }
/** @internal */
_originalMode: ChangeDetectionStrategy;

constructor(private _view: AppView<C>) {
this._view = _view;
this._originalMode = this._view.cdMode;
}

get internalView(): AppView<C> { return this._view; }

Expand All @@ -95,7 +101,7 @@ export class ViewRef_<C> implements EmbeddedViewRef<C>, ChangeDetectorRef {
detectChanges(): void { this._view.detectChanges(false); }
checkNoChanges(): void { this._view.detectChanges(true); }
reattach(): void {
this._view.cdMode = ChangeDetectionStrategy.CheckAlways;
this._view.cdMode = this._originalMode;
this.markForCheck();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,43 @@ export function main() {

expect(renderLog.log).toEqual([]);
}));

it('Reattaches', fakeAsync(() => {
var ctx = createCompFixture('<comp-with-ref></comp-with-ref>');
var cmp: CompWithRef = queryDirs(ctx.debugElement, CompWithRef)[0];

cmp.value = 'hello';
cmp.changeDetectorRef.detach();

ctx.detectChanges();

expect(renderLog.log).toEqual([]);

cmp.changeDetectorRef.reattach();

ctx.detectChanges();

expect(renderLog.log).toEqual(['{{hello}}']);

}));

it('Reattaches in the original cd mode', fakeAsync(() => {
var ctx = createCompFixture('<push-cmp></push-cmp>');
var cmp: PushComp = queryDirs(ctx.debugElement, PushComp)[0];
cmp.changeDetectorRef.detach();
cmp.changeDetectorRef.reattach();

// renderCount should NOT be incremented with each CD as CD mode should be resetted to
// on-push
ctx.detectChanges();
expect(cmp.renderCount).toBeGreaterThan(0);
var count = cmp.renderCount;

ctx.detectChanges();
expect(cmp.renderCount).toBe(count);

}));

});

describe('multi directive order', () => {
Expand Down Expand Up @@ -1192,14 +1229,20 @@ class CompWithRef {

@Component({
selector: 'push-cmp',
template: '<div (event)="noop()" emitterDirective></div>{{value}}',
template: '<div (event)="noop()" emitterDirective></div>{{value}}{{renderIncrement}}',
host: {'(event)': 'noop()'},
directives: ALL_DIRECTIVES,
pipes: ALL_PIPES,
changeDetection: ChangeDetectionStrategy.OnPush
})
class PushComp {
@Input() public value: any;
public renderCount: any = 0;

get renderIncrement() {
this.renderCount++;
return '';
}

constructor(public changeDetectorRef: ChangeDetectorRef) {}

Expand Down

0 comments on commit 773c349

Please sign in to comment.