Skip to content

Commit 91b6223

Browse files
committed
Merge branch 'bc-1.0.0' of http://github.com/wbuchwalter/ng-redux into bc-1.0.0
2 parents 9d68e56 + 2ee6b0f commit 91b6223

File tree

1 file changed

+77
-73
lines changed

1 file changed

+77
-73
lines changed

README.md

Lines changed: 77 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,26 @@ For Angular 2 see [ng2-redux](https://github.com/wbuchwalter/ng2-redux).
88
[![build status](https://img.shields.io/travis/wbuchwalter/ng-redux/master.svg?style=flat-square)](https://travis-ci.org/wbuchwalter/ng-redux)
99
[![npm version](https://img.shields.io/npm/v/ng-redux.svg?style=flat-square)](https://www.npmjs.com/package/ng-redux)
1010

11-
## Installation
12-
```js
13-
npm install --save ng-redux
14-
```
15-
16-
## Overview
1711

18-
ngRedux lets you easily connect your angular components with Redux.
19-
the API is straightforward:
20-
21-
```JS
22-
$ngRedux.connect(selector, callback);
23-
```
24-
25-
Where `selector` is a function that takes Redux's entire store state as argument and returns an object that contains the slices of store state that your component is interested in.
26-
e.g:
27-
```JS
28-
state => ({todos: state.todos})
29-
```
30-
Note: if you are not familiar with this syntax, go and check out the [MDN Guide on fat arrow functions (ES2015)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
12+
*ngRedux lets you easily connect your angular components with Redux.*
3113

32-
If you haven't, check out [reselect](https://github.com/faassen/reselect), an awesome tool to create and combine selectors.
3314

15+
## Table of Contents
3416

35-
This returned object will be passed as argument to the callback provided whenever the state changes.
36-
ngRedux checks for shallow equality of the state's selected slice whenever the Store is updated, and will call the callback only if there is a change.
37-
**Important: It is assumed that you never mutate your states, if you do mutate them, ng-redux will not execute the callback properly.**
38-
See [Redux's doc](http://gaearon.github.io/redux/docs/basics/Reducers.html) to understand why you should not mutate your states.
17+
- [Installation](#installation)
18+
- [Quick Start](#quick-start)
19+
- [API](#api)
20+
- [Using DevTools](#using-devtools)
3921

22+
## Installation
23+
```js
24+
npm install --save ng-redux
25+
```
4026

41-
## Getting Started
27+
## Quick Start
4228

4329
#### Initialization
4430

45-
```JS
46-
$ngReduxProvider.createStoreWith(reducer, [middlewares], storeEnhancer);
47-
```
48-
#### Parameters:
49-
* reducer (Function): A single reducer composed of all other reducers (create with redux.combineReducer)
50-
* [middleware] (Array of Function or String): An array containing all the middleware that should be applied. Functions and strings are both valid members. String will be resolved via Angular, allowing you to use dependency injection in your middlewares.
51-
* storeEnhancer: Optional function that will be used to create the store, in most cases you don't need that, see [Store Enhancer official doc](http://rackt.github.io/redux/docs/Glossary.html#store-enhancer)
52-
5331
```JS
5432
import reducers from './reducers';
5533
import {combineReducers} from 'redux';
@@ -69,70 +47,96 @@ angular.module('app', ['ngRedux'])
6947
return {
7048
controllerAs: 'vm',
7149
controller: TodoLoaderController,
72-
template: "<div ng-repeat='todo in vm.todos'>{{todo.text}}</div>",
73-
74-
[...]
50+
template: "<div ng-repeat='todo in vm.todos'>{{todo.text}}</div>"
7551
};
7652
}
7753

7854
class TodoLoaderController {
79-
8055
constructor($ngRedux) {
81-
this.todos = [];
82-
$ngRedux.connect(state => ({todos: state.todos}), ({todos}) => this.todos = todos);
56+
$ngRedux.connect(state => ({todos: state.todos}), this);
8357
}
84-
85-
[...]
8658
}
8759
```
8860

89-
**Note: The callback provided to `connect` will be called once directly after creation to allow initialization of your component states**
61+
## API
9062

63+
### `createStoreWith([reducer], [middlewares], [storeEnhancers])`
9164

65+
Creates the Redux store, and allow `connect()` to access it.
9266

93-
You can also grab multiple slices of the state by passing an array of selectors:
67+
#### Arguments:
68+
* [`reducer`] \(*Function*): A single reducer composed of all other reducers (create with redux.combineReducer)
69+
* [`middlewares`] \(*Function[]*): Optional, An array containing all the middleware that should be applied. Functions and strings are both valid members. String will be resolved via Angular, allowing you to use dependency injection in your middlewares.
70+
* [`storeEnhancers`] \(*Function[]*): Optional, this will be used to create the store, in most cases you don't need to pass anything, see [Store Enhancer official documentation.](http://rackt.github.io/redux/docs/Glossary.html#store-enhancer)
9471

95-
```JS
96-
constructor($ngRedux) {
97-
this.todos = [];
98-
this.users = [];
99-
$ngRedux.connect(state => ({
100-
todos: state.todos,
101-
users: state.users
102-
}),
103-
({todos, users}) => {
104-
this.todos = todos
105-
this.users = users;
106-
});
107-
}
108-
```
10972

73+
### `connect([mapStateToTarget], [target])`
11074

111-
#### Unsubscribing
75+
Connects an Angular component to Redux.
11276

113-
You can close a connection like this:
77+
#### Arguments
78+
* [`mapStateToTarget`] \(*Function*): connect will subscribe to Redux store updates. Any time it updates, mapStateToTarget will be called. Its result must be a plain object, and it will be merged into `target`.
79+
* [`target`] \(*Object*): A plain object, this will be use as a target by `mapStateToTarget`. Read the Remarks below about the implication of passing `$scope` to `connect`.
11480

115-
```JS
81+
#### Returns
82+
(*Function*): A function that unsubscribes the change listener.
11683

117-
constructor($ngRedux) {
118-
this.todos = [];
119-
this.unsubscribe = $ngRedux.connect(state => ({todos: state.todos}), ({todos}) => this.todos = todos);
120-
}
84+
#### Remarks
85+
If `$scope` is passed to `connect` as `target`, ngRedux will listen to the `$destroy` event and unsubscribe the change listener when it is triggered, you don't need to keep track of your subscribtions in this case.
86+
If anything else than `$scope` is passed as target, the responsability to unsubscribe correctly is deferred to the user.
12187

122-
destroy() {
123-
this.unsubscribe();
124-
}
88+
### Store API
89+
All of redux's store methods (i.e. `dispatch`, `subscribe` and `getState`) are exposed by $ngRedux and can be accessed directly. For example:
12590

91+
```JS
92+
redux.bindActionCreators(actionCreator, $ngRedux.dispatch);
12693
```
12794

12895

129-
#### Accessing Redux's store methods
130-
All of redux's store methods (i.e. `dispatch`, `subscribe` and `getState`) are exposed by $ngRedux and can be accessed directly. For example:
96+
## Using DevTools
97+
In order to use Redux DevTools with your angular app, you need to install [react](https://www.npmjs.com/package/react), [react-redux](https://www.npmjs.com/package/react-redux) and [redux-devtools](https://www.npmjs.com/package/redux-devtools) as development dependencies.
13198

13299
```JS
133-
redux.bindActionCreators(actionCreator, $ngRedux.dispatch);
100+
[...]
101+
import { devTools, persistState } from 'redux-devtools';
102+
import { DevTools, DebugPanel, LogMonitor } from 'redux-devtools/lib/react';
103+
import React, { Component } from 'react';
104+
105+
angular.module('app', ['ngRedux'])
106+
.config(($ngReduxProvider) => {
107+
$ngReduxProvider.createStoreWith(rootReducer, [thunk], [devTools()]);
108+
})
109+
.run(($ngRedux, $rootScope) => {
110+
React.render(
111+
<App store={ $ngRedux }/>,
112+
document.getElementById('devTools')
113+
);
114+
115+
//To reflect state changes when disabling/enabling actions via the monitor
116+
//there is probably a smarter way to achieve that
117+
$ngRedux.subscribe(_ => {
118+
setTimeout($rootScope.$apply, 100);
119+
});
120+
});
121+
122+
class App extends Component {
123+
render() {
124+
return (
125+
<div>
126+
<DebugPanel top right bottom>
127+
<DevTools store={ this.props.store } monitor = { LogMonitor } />
128+
</DebugPanel>
129+
</div>
130+
);
131+
}
132+
}
134133
```
135-
**Note:** If you choose to use `subscribe` directly, be sure to [unsubscribe](#unsubscribing) when your current scope is $destroyed.
136134

137-
### Example:
138-
An example can be found here (in TypeScript): [tsRedux](https://github.com/wbuchwalter/tsRedux/blob/master/src/components/regionLister.ts).
135+
```HTML
136+
<body>
137+
<div ng-app='app'>
138+
[...]
139+
</div>
140+
<div id="devTools"></div>
141+
</body>
142+
```

0 commit comments

Comments
 (0)