Skip to content

Commit 90cf1c5

Browse files
committed
Merge pull request #85 from deloreanjs/find_dispatcher_fix
Fix find dispatcher issue with react 0.13.x
2 parents 3bb326f + a572e63 commit 90cf1c5

File tree

6 files changed

+55
-41
lines changed

6 files changed

+55
-41
lines changed

dist/delorean.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! delorean - v0.8.7 - 2015-03-24 */
1+
/*! delorean - v0.8.7 - 2015-05-27 */
22
(function (DeLorean) {
33
'use strict';
44

@@ -33,16 +33,11 @@
3333

3434
// `__findDispatcher` is a private function for **React components**.
3535
function __findDispatcher(view) {
36-
// Provide a useful error message if no dispatcher is found in the chain
37-
if (view == null) {
38-
throw 'No dispatcher found. The DeLoreanJS mixin requires a "dispatcher" property to be passed to a component, or one of it\'s ancestors.';
36+
// Provide a useful error message if no dispatcher is found
37+
if (DeLorean.dispatcher == null) {
38+
throw 'No dispatcher found. The DeLoreanJS mixin requires a "dispatcher" has been created using Flux.createDispatcher.';
3939
}
40-
/* `view` should be a component instance. If a component don't have
41-
any dispatcher, it tries to find a dispatcher from the parents. */
42-
if (!view.props.dispatcher) {
43-
return __findDispatcher(view._owner);
44-
}
45-
return view.props.dispatcher;
40+
return DeLorean.dispatcher;
4641
}
4742

4843
// `__clone` creates a deep copy of an object.
@@ -111,10 +106,12 @@
111106
}
112107

113108
// `dispatch` method dispatch the event with `data` (or **payload**)
114-
Dispatcher.prototype.dispatch = function (actionName, data) {
115-
var self = this, stores, deferred;
109+
Dispatcher.prototype.dispatch = function () {
110+
var self = this, stores, deferred, args;
111+
args = Array.prototype.slice.call(arguments);
112+
113+
this.listener.emit.apply(this.listener, ['dispatch'].concat(args));
116114

117-
this.listener.emit('dispatch', actionName, data);
118115
/* Stores are key-value pairs. Collect store instances into an array. */
119116
stores = (function () {
120117
var stores = [], store;
@@ -131,11 +128,11 @@
131128

132129
// Store instances should wait for finish. So you can know if all the
133130
// stores are dispatched properly.
134-
deferred = this.waitFor(stores, actionName);
131+
deferred = this.waitFor(stores, args[0]);
135132

136133
/* Payload should send to all related stores. */
137134
for (var storeName in self.stores) {
138-
self.stores[storeName].dispatchAction(actionName, data);
135+
self.stores[storeName].dispatchAction.apply(self.stores[storeName], args);
139136
}
140137

141138
// `dispatch` returns deferred object you can just use **promise**
@@ -535,6 +532,16 @@
535532
}
536533
}
537534

535+
// Allow only a single dispatcher
536+
if (DeLorean.dispatcher != null) {
537+
if (console != null) {
538+
console.warn('You are attempting to create more than one dispatcher. DeLorean is intended to be used with a single dispatcher. This latest dispatcher created will overwrite any previous versions.');
539+
}
540+
}
541+
542+
// Create an internal reference to the dispathcer instance. This allows it to be found by the mixins.
543+
DeLorean.dispatcher = dispatcher;
544+
538545
return dispatcher;
539546
},
540547
// ### `DeLorean.Flux.define`
@@ -652,6 +659,13 @@
652659

653660
// `getStore` is a shortcut to get the store from the state.
654661
getStore: function (storeName) {
662+
if (console != null && typeof this.__watchStores[storeName] === 'undefined') {
663+
var message;
664+
message = 'Attempt to getStore ' + storeName + ' failed. ';
665+
message += typeof this.stores[storeName] === 'undefined' ? 'It is not defined on the dispatcher. ' : 'It is not being watched by the component. ';
666+
message += this.constructor != null && this.constructor.displayName != null ? 'Check the ' + this.constructor.displayName + ' component.' : '';
667+
console.warn(message);
668+
}
655669
return this.state.stores[storeName];
656670
}
657671
}

0 commit comments

Comments
 (0)