Comparing version 0.2.5 to 0.2.6
@@ -67,3 +67,3 @@ /** | ||
debug('executing handler for ' + storeName); | ||
handlerFn(self.payload); | ||
handlerFn(self.payload, self.name); | ||
self._isCompleted[storeName] = true; | ||
@@ -70,0 +70,0 @@ }; |
@@ -7,3 +7,4 @@ /** | ||
var Action = require('./Action'); | ||
var Action = require('./Action'), | ||
DEFAULT = 'default'; | ||
@@ -29,3 +30,5 @@ module.exports = function () { | ||
Dispatcher.stores = {}; | ||
Dispatcher.handlers = {}; | ||
Dispatcher.handlers = { | ||
'default': [] | ||
}; | ||
@@ -47,3 +50,3 @@ /** | ||
if (Dispatcher.stores[storeName]) { | ||
throw new Error('Store `' + storeName + '` is already registerd.'); | ||
throw new Error('Store `' + storeName + '` is already registered.'); | ||
} | ||
@@ -143,3 +146,5 @@ Dispatcher.stores[storeName] = store; | ||
Dispatcher.prototype.dispatch = function dispatch(actionName, payload) { | ||
if (!Dispatcher.handlers[actionName]) { | ||
var actionHandlers = Dispatcher.handlers[actionName] || [], | ||
defaultHandlers = Dispatcher.handlers[DEFAULT] || []; | ||
if (!actionHandlers.length && !defaultHandlers.length) { | ||
debug(actionName + ' does not have any registered handlers'); | ||
@@ -151,4 +156,9 @@ return; | ||
var self = this, | ||
allHandlers = actionHandlers.concat(defaultHandlers), | ||
handlerFns = {}; | ||
Dispatcher.handlers[actionName].forEach(function actionHandlersEach(store) { | ||
allHandlers.forEach(function actionHandlersEach(store) { | ||
if (handlerFns[store.name]) { | ||
// Don't call the default if the store has an explicit action handler | ||
return; | ||
} | ||
var storeInstance = self.getStore(store.name); | ||
@@ -155,0 +165,0 @@ if ('function' === typeof store.handler) { |
{ | ||
"name": "dispatchr", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"description": "A Flux dispatcher for applications that run on the server and the client.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -70,9 +70,4 @@ # Dispatchr [![Build Status](https://travis-ci.org/yahoo/dispatchr.svg?branch=master)](https://travis-ci.org/yahoo/dispatchr) [![Dependency Status](https://david-dm.org/yahoo/dispatchr.svg)](https://david-dm.org/yahoo/dispatchr) [![Coverage Status](https://coveralls.io/repos/yahoo/dispatchr/badge.png?branch=master)](https://coveralls.io/r/yahoo/dispatchr?branch=master) | ||
We have provided utilities for creating stores in a couple of forms: | ||
We have provided [utilities for creating stores](#helper-utilities) but you are not required to use these if you want to keep your stores completely decoupled from the dispatcher. Dispatchr only expects that your stores use the following interface: | ||
* `require('dispatchr/utils/createStore')` returns a `React.createClass`-like function for creating stores. | ||
* `require('dispatchr/utils/BaseStore')` returns an extendable class. | ||
You are not required to use these if you want to keep your stores completely decoupled from the dispatcher. Dispatchr expects that your stores use the following interface: | ||
### Constructor | ||
@@ -119,12 +114,14 @@ | ||
ExampleStore.handlers = { | ||
'NAVIGATE': 'handleNavigate' | ||
'NAVIGATE': 'handleNavigate', | ||
'default': 'defaultHandler' // Called for any action that has not been otherwise handled | ||
}; | ||
``` | ||
The handler function will be passed one parameter: | ||
The handler function will be passed two parameters: | ||
* `payload`: An object containing action information. | ||
* `actionName`: The name of the action. This is primarily useful when using the `default` handler | ||
```js | ||
ExampleStore.prototype.handleNavigate = function (payload) { | ||
ExampleStore.prototype.handleNavigate = function (payload, actionName) { | ||
this.navigating = true; | ||
@@ -139,3 +136,3 @@ this.emit('change'); // Component may be listening for changes to state | ||
ExampleStore.handlers = { | ||
'NAVIGATE': function handleNavigate(payload) { | ||
'NAVIGATE': function handleNavigate(payload, actionName) { | ||
// bound to store instance | ||
@@ -170,2 +167,42 @@ this.navigating = true; | ||
## Helper Utilities | ||
These utilities make creating stores less verbose and provide some `change` related functions that are common amongst all store implementations. | ||
### BaseStore | ||
`require('dispatchr/utils/BaseStore')` provides a base store class for extending. Provides `emitChange`, `addChangeListener`, and `removeChangeListener` functions. Example: | ||
```js | ||
var util = require('util'); | ||
var BaseStore = require('dispatchr/utils/BaseStore'); | ||
var MyStore = function (dispatcherInterface) { | ||
BaseStore.apply(this, arguments); | ||
}; | ||
util.inherits(MyStore, BaseStore); | ||
MyStore.storeName = 'MyStore'; | ||
MyStore.handlers = { | ||
'NAVIGATE': function (payload) { ... this.emitChange() ... } | ||
}; | ||
MyStore.prototype.getFoo = function () { ... } | ||
module.exports = MyStore; | ||
``` | ||
### createStore | ||
`require('dispatchr/utils/createStore')` provides a helper function for creating stores similar to React's `createClass` function. The created store class will extend BaseStore and have the same built-in functions. Example: | ||
```js | ||
var createStore = require('dispatchr/utils/createStore'); | ||
var MyStore = createStore({ | ||
initialize: function () {}, // Called immediately after instantiation | ||
storeName: 'MyStore', | ||
handlers: { | ||
'NAVIGATE': function (payload) { ... this.emitChange() ... } | ||
} | ||
foo: function () { ... } | ||
}); | ||
module.exports = MyStore; | ||
``` | ||
## License | ||
@@ -172,0 +209,0 @@ |
Sorry, the diff of this file is not supported yet
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
67822
421
210