Skip to content

Commit 86de39a

Browse files
committed
Update README.md
1 parent c591e50 commit 86de39a

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

README.md

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
For Angular 2 see [ng2-redux](https://github.com/wbuchwalter/ng2-redux).
55

6-
**Warning: The API will be subject to breaking changes until `1.0.0` is released. You can follow progress on the `bc-1.0.0` branch**
6+
**Warning: The API will be subject to breaking changes until `1.0.0` is released.**
77

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)
@@ -20,6 +20,8 @@ For Angular 2 see [ng2-redux](https://github.com/wbuchwalter/ng2-redux).
2020
- [Using DevTools](#using-devtools)
2121

2222
## Installation
23+
24+
**The current npm version is outdated, and will be updated once 1.0.0 is finished**
2325
```js
2426
npm install --save ng-redux
2527
```
@@ -30,7 +32,7 @@ npm install --save ng-redux
3032

3133
```JS
3234
import reducers from './reducers';
33-
import {combineReducers} from 'redux';
35+
import { combineReducers } from 'redux';
3436
import loggingMiddleware from './loggingMiddleware';
3537
import 'ng-redux';
3638

@@ -42,18 +44,21 @@ angular.module('app', ['ngRedux'])
4244
```
4345

4446
#### Usage
47+
*Note: this sample is using the ControllerAs syntax, usage is slightly different without ControllerAs, see API section for more details*
48+
4549
```JS
46-
export default function todoLoader() {
47-
return {
48-
controllerAs: 'vm',
49-
controller: TodoLoaderController,
50-
template: "<div ng-repeat='todo in vm.todos'>{{todo.text}}</div>"
51-
};
52-
}
50+
import * as CounterActions from '../actions/counter';
5351

54-
class TodoLoaderController {
55-
constructor($ngRedux) {
56-
$ngRedux.connect(state => ({todos: state.todos}), this);
52+
class CounterController {
53+
constructor($ngRedux, $scope) {
54+
$ngRedux.connect($scope, this.mapStateToScope, CounterActions, 'vm');
55+
}
56+
57+
// Which part of the Redux global state does our component want to receive on $scope?
58+
mapStateToScope(state) {
59+
return {
60+
counter: state.counter
61+
};
5762
}
5863
}
5964
```
@@ -70,20 +75,18 @@ Creates the Redux store, and allow `connect()` to access it.
7075
* [`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)
7176

7277

73-
### `connect([mapStateToTarget], [target])`
78+
### `connect([scope], [mapStateToScope], [mapDispatchToScope], [propertyKey])`
7479

7580
Connects an Angular component to Redux.
7681

7782
#### 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`.
80-
81-
#### Returns
82-
(*Function*): A function that unsubscribes the change listener.
83+
* [`scope`] \(*Object*): The `$scope` of your controller.
84+
* [`mapStateToScope`] \(*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`.
85+
* [`mapDispatchToScope`] \(*Object* or *Function*): If an object is passed, each function inside it will be assumed to be a Redux action creator. An object with the same function names, but bound to a Redux store, will be merged into your component `$scope`. If a function is passed, it will be given `dispatch`. It’s up to you to return an object that somehow uses `dispatch` to bind action creators in your own way. (Tip: you may use the [`bindActionCreators()`](http://gaearon.github.io/redux/docs/api/bindActionCreators.html) helper from Redux.).
86+
* [`propertyKey`] \(*string*): If provided, `mapStateToScope` and `mapDispatchToScope` will merge onto `$scope[propertyKey]`. This is needed especially when using the `ControllerAs` syntax: in this case you should provide the same value than the value provided to controllerAs (e.g: `'vm'`). When not using `ControllerAs` syntax, you are free to omit this argument, everything will be merged directly onto `$scope`.
8387

8488
#### 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.
89+
As `$scope` is passed to `connect`, ngRedux will listen to the `$destroy` event and unsubscribe the change listener itself, you don't need to keep track of your subscribtions.
8790

8891
### Store API
8992
All of redux's store methods (i.e. `dispatch`, `subscribe` and `getState`) are exposed by $ngRedux and can be accessed directly. For example:
@@ -92,6 +95,8 @@ All of redux's store methods (i.e. `dispatch`, `subscribe` and `getState`) are e
9295
redux.bindActionCreators(actionCreator, $ngRedux.dispatch);
9396
```
9497

98+
This means that you are free to use Redux basic API in advanced cases where `connect`'s API would not fill your needs.
99+
95100

96101
## Using DevTools
97102
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.

0 commit comments

Comments
 (0)