Skip to content

Commit cac1ac2

Browse files
committed
Working example for new API
1 parent bec2e64 commit cac1ac2

File tree

4 files changed

+43
-43
lines changed

4 files changed

+43
-43
lines changed

examples/counter/containers/CounterApp.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Component, View, onInit, onDestroy} from 'angular2/angular2';
22
import {bindActionCreators} from 'redux';
33
import {Counter} from '../components/Counter';
44
import * as CounterActions from '../actions/CounterActions';
5-
import Connector from '../redux/connector';
5+
import Connector from '../redux/connector';
66
import { Inject } from 'angular2/di';
77

88
@Component({
@@ -11,28 +11,29 @@ import { Inject } from 'angular2/di';
1111
bindings: []
1212
})
1313
@View({
14-
directives: [ Counter ],
14+
directives: [Counter],
1515
template: `
1616
<counter [counter]="counter" [actions]="actions"></counter>
1717
`
1818
})
1919
export class CounterApp {
20-
actions: any;
21-
unsubscribe: Function;
22-
counter;
23-
_ngRedux;
24-
25-
constructor(@Inject('ngRedux') ngRedux) {
26-
this._ngRedux = ngRedux;
27-
ngRedux.connect(state => state.counter, counter => this.counter = counter, true);
20+
constructor( @Inject('ngRedux') ngRedux) {
21+
this.unsubscribe = ngRedux.connect(this.mapStateToScope, this.mapDispatchToProps)(this);
2822
}
2923

24+
onInit() {}
3025

31-
onInit() {
32-
this.actions = bindActionCreators(CounterActions, this._ngRedux.getStore().dispatch);
26+
onDestroy() {
27+
this.unsubscribe();
3328
}
3429

35-
onDestroy() {
36-
this._ngRedux.disconnect();
30+
mapStateToScope(state) {
31+
return {
32+
counter: state.counter
33+
};
34+
}
35+
36+
mapDispatchToProps(dispatch) {
37+
return { actions: bindActionCreators(CounterActions, dispatch) };
3738
}
3839
}

examples/counter/index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ import {bind} from 'angular2/di';
33
import {createStore, applyMiddleware} from 'redux';
44
import thunk from 'redux-thunk';
55
import {App} from './containers/App';
6-
import ngRedux from 'ng2-redux';
6+
import {createRedux} from 'ng2-redux';
77
import {rootReducer} from './reducers';
88

99
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
1010
const store = createStoreWithMiddleware(rootReducer);
1111

12-
console.log(ngRedux);
13-
1412
bootstrap(
1513
App,
16-
[ngRedux(store)]
17-
);
14+
[createRedux(store)]
15+
);

src/connector.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ export default class Connector {
88
constructor(store) {
99
this._store = store;
1010
this._defaultMapStateToTarget = () => ({});
11-
this._defaultMapDispatchToTarget = dispatch => ({dispatch});
11+
this._defaultMapDispatchToTarget = dispatch => ({ dispatch });
1212
}
1313

14-
connect(mapStateToTarget, mapDispatchToTarget) {
14+
connect = (mapStateToTarget, mapDispatchToTarget) => {
1515

1616
const finalMapStateToTarget = mapStateToTarget || this._defaultMapStateToTarget;
1717

@@ -56,24 +56,24 @@ export default class Connector {
5656
}
5757

5858

59-
updateTarget(target, StateSlice, dispatch) {
60-
if(_.isFunction(target)) {
61-
target(StateSlice, dispatch);
62-
} else {
63-
_.assign(target, StateSlice, dispatch);
59+
updateTarget(target, StateSlice, dispatch) {
60+
if (_.isFunction(target)) {
61+
target(StateSlice, dispatch);
62+
} else {
63+
_.assign(target, StateSlice, dispatch);
64+
}
6465
}
65-
}
6666

67-
getStateSlice(state, mapStateToScope) {
68-
const slice = mapStateToScope(state);
67+
getStateSlice(state, mapStateToScope) {
68+
const slice = mapStateToScope(state);
6969

70-
invariant(
71-
_.isPlainObject(slice),
72-
'`mapStateToScope` must return an object. Instead received %s.',
73-
slice
74-
);
70+
invariant(
71+
_.isPlainObject(slice),
72+
'`mapStateToScope` must return an object. Instead received %s.',
73+
slice
74+
);
7575

76-
return slice;
77-
}
76+
return slice;
77+
}
7878

7979
}

src/index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
import Connector from './connector';
22
import {bind, Injector} from 'angular2/di';
3-
let redux = import('redux');
3+
let redux = require('redux');
44

55
export function createRedux(store) {
66
const _connector = new Connector(store);
77

88
return bind('ngRedux').toFactory(() => {
9-
return {connect: _connector.connect, ...store};
9+
return { connect: _connector.connect, ...store};
1010
});
1111
}
1212

1313

1414
/*
1515
const createStoreWithMiddleware = applyInjectableMiddleware(thunk, 'promise')(createStore);
1616
*/
17-
17+
/*
1818
export function applyInjectableMiddleware(middlewares) {
1919
const injector = new Injector();
2020
let resolvedMiddlewares = [];
21-
_.forEach(middlewares, middleware => {
22-
_.isString(middleware)
23-
? resolvedMiddlewares.push(Injector.resolve(middleware))
21+
_.forEach(middlewares, middleware => {
22+
_.isString(middleware)
23+
? resolvedMiddlewares.push(Injector.resolve(middleware))
2424
: resolvedMiddlewares.push(middleware)
2525
});
26-
26+
2727
return redux.applyMiddleware(...resolvedMiddlewares);
2828
}
29+
*/
2930

3031

3132

0 commit comments

Comments
 (0)