Skip to content

Instantly share code, notes, and snippets.

@johnlindquist
Created March 15, 2016 15:54
Show Gist options
  • Save johnlindquist/366ac2d7fd830f0641d3 to your computer and use it in GitHub Desktop.
Save johnlindquist/366ac2d7fd830f0641d3 to your computer and use it in GitHub Desktop.
Angular2 + Typescript Demo @ngrx/store 1.3.3 Beta.9

Angular2 Starter Plunker - Typescript - Beta9

System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
//map tells the System loader where to look for things
map: {
app: './src',
'@ngrx/store': 'https://npmcdn.com/@ngrx/[email protected]/dist/index.js'
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
}
}
});
<!DOCTYPE html>
<html>
<head>
<title>angular2 playground</title>
<link rel="stylesheet" href="style.css" />
<script src="https://code.angularjs.org/2.0.0-beta.9/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://code.angularjs.org/tools/typescript.js"></script>
<script src="config.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.9/Rx.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.9/angular2.dev.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.9/http.dev.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.9/router.dev.js"></script>
<script>
System.import('app')
.catch(console.error.bind(console));
</script>
</head>
<body>
<my-app>
loading...
</my-app>
</body>
</html>
import {Component} from 'angular2/core';
import {Store} from '@ngrx/store';
import {AppStore, CounterActions, CounterSlice, SpecialPayload} from './main';
@Component({
selector: 'my-app',
template: `
<button (click)="increment()">Increment</button>
<div>Current Count: {{ counterStream.counter | async }}</div>
<div>Special Payload: {{ counterStream.special | async }}</div>
<div>
<button (click)="decrement()">Decrement</button>
<button (click)="reset()">Reset</button>
`
})
export class App {
counterStream: Observable<CounterSlice>;
anotherStream: Observable<CounterSlice>;
constructor(public store: Store<AppStore>){
this.counterStream = store.select(slice => slice.counter);
this.anotherStream = store.select(slice => slice.counter).subscribe(
x => {
console.log('Next: ' + JSON.stringify(x) || "xxundefinedxx");
},
err => {
console.log('Error: %s', err);
},
() => {
console.log('Completed');
}
);
}
increment(){
let counterAction: Action = {
type: CounterActions[CounterActions.IncrementCounter],
payload: {
counter: ++this.counter,
special: SpecialPayload.FOO}
};
this.store.dispatch(counterAction);
}
decrement(){
let counterAction: Action = {
type: CounterActions[CounterActions.DecrementCounter],
payload: {
counter: --this.counter,
special: SpecialPayload.BAR}
};
this.store.dispatch(counterAction);
}
reset(){
let counterAction: Action = {
type: CounterActions[CounterActions.ResetCounter],
payload: {
counter: 55,
special: SpecialPayload.FOO}
};
this.store.dispatch(counterAction);
}
}
import {Reducer, Action} from '@ngrx/store';
import {CounterActions, CounterSlice, SpecialPayload} from './main';
// create reducer with default value specified
export const counterReducer: Reducer<CounterSlice> = (
state: CounterSlice = {
counter: 42,
special: SpecialPayload.FOO},
action: Action) => {
// action will be the type broadcasted via the dispatch
// payload is an arbitrary chunk of data to send along with our action for processing
console.log('COUNTER received action/payload ' + JSON.stringify(action));
// product here is the CURRENT state of the store for this reducer type
console.log('COUNTER current store value for platform ' + JSON.stringify(state));
// in this switch we are returning the new value for the store to then commit internally
/* tslint:disable:align */
switch (action.type) {
case CounterActions[CounterActions.IncrementCounter]:
return {
counter: state.counter + 1,
special: SpecialPayload.BAR
};
case CounterActions[CounterActions.DecrementCounter]:
return {
counter: state.counter - 1,
special: SpecialPayload.FOO
};
case CounterActions[CounterActions.ResetCounter]:
return {
counter: 42,
special: SpecialPayload.FOO
};
default:
return state;
};
/* tslint:enable:align */
};
//main entry point
import {bootstrap} from 'angular2/platform/browser';
import {App} from './app';
import {provideStore} from '@ngrx/store';
import 'rxjs/Rx';
import {counterReducer} from './counter';
export enum SpecialPayload { FOO, BAR };
export enum CounterActions { IncrementCounter, DecrementCounter, ResetCounter }
export interface CounterSlice {
counter: CounterActions;
special: SpecialPayload;
};
export interface AppStore {
counter: CounterSlice;
};
bootstrap(App, [
provideStore({counterReducer})
]);
/* Styles go here */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment