|
1 | | -/*! delorean - v0.8.7 - 2015-03-24 */ |
| 1 | +/*! delorean - v0.8.7 - 2015-05-27 */ |
2 | 2 | (function (DeLorean) { |
3 | 3 | 'use strict'; |
4 | 4 |
|
|
33 | 33 |
|
34 | 34 | // `__findDispatcher` is a private function for **React components**. |
35 | 35 | 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.'; |
39 | 39 | } |
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; |
46 | 41 | } |
47 | 42 |
|
48 | 43 | // `__clone` creates a deep copy of an object. |
|
111 | 106 | } |
112 | 107 |
|
113 | 108 | // `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)); |
116 | 114 |
|
117 | | - this.listener.emit('dispatch', actionName, data); |
118 | 115 | /* Stores are key-value pairs. Collect store instances into an array. */ |
119 | 116 | stores = (function () { |
120 | 117 | var stores = [], store; |
|
131 | 128 |
|
132 | 129 | // Store instances should wait for finish. So you can know if all the |
133 | 130 | // stores are dispatched properly. |
134 | | - deferred = this.waitFor(stores, actionName); |
| 131 | + deferred = this.waitFor(stores, args[0]); |
135 | 132 |
|
136 | 133 | /* Payload should send to all related stores. */ |
137 | 134 | for (var storeName in self.stores) { |
138 | | - self.stores[storeName].dispatchAction(actionName, data); |
| 135 | + self.stores[storeName].dispatchAction.apply(self.stores[storeName], args); |
139 | 136 | } |
140 | 137 |
|
141 | 138 | // `dispatch` returns deferred object you can just use **promise** |
|
535 | 532 | } |
536 | 533 | } |
537 | 534 |
|
| 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 | + |
538 | 545 | return dispatcher; |
539 | 546 | }, |
540 | 547 | // ### `DeLorean.Flux.define` |
|
652 | 659 |
|
653 | 660 | // `getStore` is a shortcut to get the store from the state. |
654 | 661 | 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 | + } |
655 | 669 | return this.state.stores[storeName]; |
656 | 670 | } |
657 | 671 | } |
|
0 commit comments