Skip to content

Commit ff97b23

Browse files
committed
build: add toggle for OnPush change detection in demo app
Adds a toggle for enabling `OnPush` change detection in the demo app. This should make it easier to test out components with the different change detection strategies.
1 parent f15619c commit ff97b23

File tree

5 files changed

+53
-14
lines changed

5 files changed

+53
-14
lines changed

src/demo-app/demo-app-module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {HttpModule} from '@angular/http';
44
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
55
import {RouterModule} from '@angular/router';
66
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
7-
import {DemoApp, Home} from './demo-app/demo-app';
7+
import {DemoApp, Home, DemoAppOnPush} from './demo-app/demo-app';
88
import {
99
MaterialModule,
1010
OverlayContainer,
@@ -65,6 +65,7 @@ import {StyleDemo} from './style/style-demo';
6565
ChipsDemo,
6666
CheckboxDemo,
6767
DemoApp,
68+
DemoAppOnPush,
6869
DialogDemo,
6970
GesturesDemo,
7071
GridListDemo,

src/demo-app/demo-app/demo-app.html

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,28 @@
2525
</button>
2626
<div class="demo-toolbar">
2727
<h1>Angular Material Demos</h1>
28-
<button md-button (click)="toggleFullscreen()" title="Toggle fullscreen">
29-
Fullscreen
30-
</button>
31-
<button md-button (click)="root.dir = (root.dir == 'rtl' ? 'ltr' : 'rtl')" title="Toggle between RTL and LTR">
32-
{{root.dir.toUpperCase()}}
33-
</button>
28+
<div>
29+
<button md-button (click)="toggleChangeDetection()" title="Toggle change detection">
30+
Change detection: {{changeDetectionStrategy}}
31+
</button>
32+
<button md-button (click)="toggleFullscreen()" title="Toggle fullscreen">
33+
Fullscreen
34+
</button>
35+
<button md-button (click)="root.dir = (root.dir == 'rtl' ? 'ltr' : 'rtl')" title="Toggle between RTL and LTR">
36+
{{root.dir.toUpperCase()}}
37+
</button>
38+
</div>
3439
</div>
3540
</md-toolbar>
3641

37-
<div #root="$implicit" dir="ltr" class="demo-content">
38-
<router-outlet></router-outlet>
42+
<div #root="$implicit" dir="ltr" class="demo-content" [ngSwitch]="changeDetectionStrategy">
43+
<div *ngSwitchDefault>
44+
<router-outlet></router-outlet>
45+
</div>
46+
47+
<demo-app-on-push *ngSwitchCase="'OnPush'">
48+
<router-outlet></router-outlet>
49+
</demo-app-on-push>
3950
</div>
4051
</div>
4152
</md-sidenav-container>

src/demo-app/demo-app/demo-app.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ body {
3232
.demo-toolbar {
3333
display: flex;
3434
justify-content: space-between;
35+
align-items: center;
3536
width: 100%;
3637
}
3738
}

src/demo-app/demo-app/demo-app.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
import {Component, ViewEncapsulation, ElementRef} from '@angular/core';
1+
import {Component, ViewEncapsulation, ElementRef, ChangeDetectionStrategy} from '@angular/core';
22

3+
const changeDetectionKey = 'mdDemoChangeDetection';
34

45
@Component({
56
selector: 'home',
67
template: `
78
<p>Welcome to the development demos for Angular Material!</p>
8-
<p>Open the sidenav to select a demo. </p>
9+
<p>Open the sidenav to select a demo.</p>
910
`
1011
})
1112
export class Home {}
1213

14+
@Component({
15+
moduleId: module.id,
16+
selector: 'demo-app-on-push',
17+
template: '<ng-content></ng-content>',
18+
changeDetection: ChangeDetectionStrategy.OnPush,
19+
encapsulation: ViewEncapsulation.None,
20+
})
21+
export class DemoAppOnPush {}
22+
1323
@Component({
1424
moduleId: module.id,
1525
selector: 'demo-app',
@@ -18,6 +28,7 @@ export class Home {}
1828
encapsulation: ViewEncapsulation.None,
1929
})
2030
export class DemoApp {
31+
changeDetectionStrategy: string;
2132
navItems = [
2233
{name: 'Autocomplete', route: 'autocomplete'},
2334
{name: 'Button', route: 'button'},
@@ -52,7 +63,12 @@ export class DemoApp {
5263
];
5364

5465
constructor(private _element: ElementRef) {
55-
66+
// Some browsers will throw when trying to access localStorage in incognito.
67+
try {
68+
this.changeDetectionStrategy = localStorage.getItem(changeDetectionKey) || 'Default';
69+
} catch (error) {
70+
console.error(error);
71+
}
5672
}
5773

5874
toggleFullscreen() {
@@ -67,4 +83,15 @@ export class DemoApp {
6783
elem.msRequestFullScreen();
6884
}
6985
}
86+
87+
toggleChangeDetection() {
88+
try {
89+
this.changeDetectionStrategy = this.changeDetectionStrategy === 'Default' ?
90+
'OnPush' : 'Default';
91+
localStorage.setItem(changeDetectionKey, this.changeDetectionStrategy);
92+
location.reload();
93+
} catch (error) {
94+
console.error(error);
95+
}
96+
}
7097
}

src/demo-app/tooltip/tooltip-demo.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, ChangeDetectionStrategy} from '@angular/core';
1+
import {Component} from '@angular/core';
22
import {TooltipPosition} from '@angular/material';
33

44

@@ -7,7 +7,6 @@ import {TooltipPosition} from '@angular/material';
77
selector: 'tooltip-demo',
88
templateUrl: 'tooltip-demo.html',
99
styleUrls: ['tooltip-demo.css'],
10-
changeDetection: ChangeDetectionStrategy.OnPush // make sure tooltip also works OnPush
1110
})
1211
export class TooltipDemo {
1312
position: TooltipPosition = 'below';

0 commit comments

Comments
 (0)