// The Flux architecture style code is in this file // Please note that these are not split into separate modules due to: // 1) small nature of this example // 2) limitations of gists // Additionally, most of this code is derived from the Flux + React.js TodoMVC // for more information, and to properly learn about the concepts I recommend // reviewing the tutorial or code there: // http://facebook.github.io/react/docs/flux-todo-list.html // https://github.com/facebook/react/tree/master/examples/todomvc-flux // Dispatching (Additionally, this could be handled using d3.dispatch // (e.g: http://bl.ocks.org/milroc/10606529)) var Dispatcher = (function() { var _callbacks = []; var _promises = []; var _addPromise = function(callback, payload) { _promises.push(new Promise(function(resolve, reject) { if (callback(payload)) { resolve(payload); } else { reject(new Error('Dispatcher callback unsuccessful')); } })); }; var _clearPromises = function() { _promises = []; }; var Dispatcher = function() {}; Dispatcher.prototype.register = function(callback) { _callbacks.push(callback); return _callbacks.length - 1; // index }; Dispatcher.prototype.dispatch = function(payload) { _callbacks.forEach(function(callback) { _addPromise(callback, payload); }); Promise.all(_promises).then(_clearPromises); }; return Dispatcher; })(); var AppDispatcher = (function() { var AppDispatcher = function() {}; AppDispatcher.prototype = new Dispatcher(); AppDispatcher.prototype.handleViewAction = function(action) { this.dispatch({ source: 'VIEW_ACTION', action: action }); }; return new AppDispatcher(); // a bit of a hack })(); // Actions var ChartActions = { range: function(extent, x) { AppDispatcher.handleViewAction({ actionType: 'range', extent: extent, x: x, }); }, update: function() { AppDispatcher.handleViewAction({ actionType: 'update', }); } };