Advertisement
arxeiss

Dispatcher and Emitter

May 19th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var dispatcher = new Flux.Dispatcher();
  2. var emitter = new EventEmitter();
  3. var DataStore = function() {
  4.     this._data = null;
  5.     Spamler.dispatcher.register(function(payload) {
  6.         switch (payload.type) {
  7.             case "loadAll":
  8.                 this._all();
  9.                 break;
  10.         }
  11.     }.bind(this));
  12.     this._all = function () {
  13.         // Load all into this._data
  14.         emitter.emit("allDataReceived", this._data);
  15.     }.bind(this);
  16. }
  17. var DataView = React.createClass({
  18.     getInitialState: function() {
  19.         return {data: null};
  20.     },
  21.     componentWillMount: function() {
  22.         emitter.on("allDataReceived", this.dataReceived);
  23.     },
  24.     componentDidMount: function() {
  25.         dispatcher.dispatch({ type: "loadAll" });
  26.     },
  27.     componentWillUnmount: function() {
  28.         emitter.off("allDataReceived", this.dataReceived);
  29.     },
  30.     dataReceived: function(data){
  31.         this.setProps({data: data});
  32.     },
  33.     render: function() {
  34.         return <div>{this.state.data}</div>
  35.     }
  36. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement