Comparing version 0.0.11 to 0.0.12
@@ -178,3 +178,3 @@ /** | ||
setImmediate(self.next.bind(self)); | ||
}).catch(function (err) { | ||
})['catch'](function (err) { | ||
debug('finished with error ' + nextAction.name); | ||
@@ -262,3 +262,3 @@ self.currentAction = null; | ||
}); | ||
}).catch(function (err) { | ||
})['catch'](function (err) { | ||
callback(err); | ||
@@ -265,0 +265,0 @@ }); |
{ | ||
"name": "dispatchr", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "A Flux dispatcher for applications that run on the server and the client.", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"type": "git", | ||
"url": "git://github.com/mridgway/dispatchr.git" | ||
"url": "git://github.com/ouchtown/dispatchr.git" | ||
}, | ||
@@ -20,3 +20,3 @@ "scripts": { | ||
"type": "BSD", | ||
"url": "https://github.com/yahoo/dispatchr/blob/master/LICENSE.md" | ||
"url": "https://github.com/ouchtown/dispatchr/blob/master/LICENSE.md" | ||
} | ||
@@ -23,0 +23,0 @@ ], |
141
README.md
@@ -1,35 +0,122 @@ | ||
Dispatchr [![Build Status](https://travis-ci.org/mridgway/dispatchr.svg?branch=master)](https://travis-ci.org/mridgway/dispatchr) [![Dependency Status](https://david-dm.org/mridgway/dispatchr.svg)](https://david-dm.org/mridgway/dispatchr) | ||
========= | ||
# Dispatchr [![Build Status](https://travis-ci.org/ouchtown/dispatchr.svg?branch=master)](https://travis-ci.org/ouchtown/dispatchr) [![Dependency Status](https://david-dm.org/ouchtown/dispatchr.svg)](https://david-dm.org/ouchtown/dispatchr) | ||
A [Flux](http://facebook.github.io/react/docs/flux-overview.html) dispatcher for applications that run on the server and the client. | ||
Usage | ||
----- | ||
## Usage | ||
For a more detailed example, see our [example application](https://github.com/mridgway/dispatchr/tree/master/examples/simple). | ||
For a more detailed example, see our [example application](https://github.com/ouchtown/flux-example). | ||
Let's start with a store that can handle the actions: | ||
```js | ||
var Dispatchr = require('dispatchr'), | ||
ExampleStore = require('./example-store.js'), | ||
context = {}; | ||
Dispatchr.registerStore(ExampleStore); | ||
var dispatcher = new Dispatchr(context); | ||
dispatcher.dispatch('NAVIGATE', {}, function (err) { | ||
// Action has been handled fully | ||
}); | ||
``` | ||
## Dispatcher Interface | ||
### registerStore(store) | ||
A static method to register stores to Dispatchr making them available to handle actions and be accessible through `getStore` on Dispatchr instances. | ||
### Constructor | ||
Creates a new instance of Dispatchr with the following parameters: | ||
* `context`: A context object that will be made available to all stores. Useful for request or session level settings. | ||
### dispatch(actionName, payload, callback) | ||
Dispatches an action, in turn calling all stores that have registered to handle this action. | ||
* `actionName`: The name of the action to handle (should map to store action handlers) | ||
* `payload`: An object containing action information. | ||
* `callback`: A function that will be called when the action has been fully handled by all stores | ||
### getStore(storeName) | ||
Retrieve a store instance by name. Allows access to stores from components or stores from other stores. | ||
### waitFor(stores, callback) | ||
Waits for another store's handler to finish before calling the callback. This is useful from within stores if they need to wait for other stores to finish first. | ||
* `stores`: A string or array of strings of store names to wait for | ||
* `callback`: Called after all stores have fully handled the action | ||
### toJSON() | ||
Returns a serializable object containing the state of the Dispatchr instance as well as all stores that have been used since instantiation. This is useful for serializing the state of the application to send it to the client. | ||
### rehydrate(dispatcherState) | ||
Takes an object representing the state of the Dispatchr instance (usually retrieved from toJSON) to rehydrate the instance as well as the store instance state. | ||
## Store Interface | ||
Dispatchr expects that your stores use the following interface: | ||
### Constructor | ||
The store should have a constructor function that will be used to instantiate your store using `new Store(context, initialState)` where the parameters are as follows: | ||
* `context`: The context object that was passed to Dispatchr's constructor. | ||
* `initialState`: The initialState of the store (generally used during rehydration) | ||
```js | ||
var util = require('util'), | ||
EventEmitter = require('events').EventEmitter; | ||
function ExampleStore(context) { | ||
function ExampleStore(context, initialState) { | ||
this.navigating = false; | ||
} | ||
``` | ||
ExampleStore.storeName = 'ExampleStore'; | ||
It is also recommended to extend an event emitter so that your store can emit `update` events to the components. | ||
```js | ||
util.inherits(ExampleStore, EventEmitter); | ||
``` | ||
### storeName | ||
The store should define a static property that gives the name of the store. This is used for accessing stores from the dispatcher via `dispatcher.getStore(name)`. | ||
```js | ||
ExampleStore.storeName = 'ExampleStore'; | ||
``` | ||
### handlers | ||
The store should define a static property that maps action names to handler function names. These functions will be called in the event that an action has been dispatched by the Dispatchr instance. | ||
```js | ||
ExampleStore.handlers = { | ||
'NAVIGATE': 'handleNavigate' | ||
}; | ||
``` | ||
ExampleStore.prototype.handleNavigate = function () { | ||
The handler function will be passed two parameters: | ||
* `payload`: An object containing action information. | ||
* `done`: A function to be called when the action has been fully handled by the store | ||
```js | ||
ExampleStore.prototype.handleNavigate = function (payload, done) { | ||
this.navigating = true; | ||
this.emit('update'); // Store may be listening for updates to state | ||
this.emit('final'); // Action has been fully handled | ||
this.emit('update'); // Component may be listening for updates to state | ||
done(); // Action has been fully handled | ||
}; | ||
``` | ||
### getState() | ||
The store should implement this function that will return a serializable data object that can be transferred from the server to the client and also be used by your components. | ||
```js | ||
ExampleStore.prototype.getState = function () { | ||
@@ -42,23 +129,21 @@ return { | ||
Now let's initialize our dispatcher and dispatch an action: | ||
### setDispatcher(dispatcher) | ||
The store can optionally define this function that will receive the Dispatchr instance after instantiation of the store. This gives the store access to functions like `waitFor` and `getStore` to call other stores. | ||
```js | ||
var Dispatchr = require('dispatchr'), | ||
ExampleStore = require('./example-store.js'), | ||
context = {}; | ||
ExampleStore.prototype.setDispatcher = function (dispatcher) { | ||
this.dispatcher = dispatcher; | ||
}; | ||
``` | ||
Dispatchr.registerStore(ExampleStore); | ||
### toJSON() | ||
var dispatcher = new Dispatchr(context); | ||
The store can optionally define this function to customize the dehydration of the store. It should return a serializable data object that will be passed to the client. | ||
dispatcher.dispatch('NAVIGATE', {}, function () { | ||
// Action has been handled fully | ||
}); | ||
``` | ||
## License | ||
License | ||
------- | ||
This software is free to use under the Yahoo! Inc. BSD license. | ||
See the [LICENSE file][] for license text and copyright information. | ||
[LICENSE file]: https://github.com/mridgway/dispatchr/blob/master/LICENSE.md | ||
[LICENSE file]: https://github.com/ouchtown/dispatchr/blob/master/LICENSE.md |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50183
149