React-Flux:错误 AppDispatcher.register

React-Flux: Error with AppDispatcher.register

我正在尝试在 Flux-React 中设置最基本的应用程序。它的唯一目标是触发一个动作,该动作通过 Dispatcher 发送到已向 Dispatcher 注册的 Store。存储将 payload 记录到控制台。

除了 Store 之外的所有东西都运行良好,但是一旦它点击 AppDispatcher.register,Flux 就会抛出以下错误:

Uncaught TypeError: Cannot set property 'ID_1' of undefined

这是导致错误的文件代码,但我已将整个项目放在 https://github.com/bengrunfeld/react-flux-dispatcher-error, and you can find the offending file in src/js/stores/AppStores.js

var AppDispatcher = require('../dispatcher/AppDispatcher');
var EventEmitter = require('events').EventEmitter;
var AppConstants = require('../constants/AppConstants');
var assign = require('object-assign');


var CHANGE_EVENT = 'change';

var AppStore = assign({}, EventEmitter.prototype, {
  emitChange: function() {
    this.emit(CHANGE_EVENT);
  }
});

AppDispatcher.register(function(payload){
  console.log(payload);
  return true;
})

module.exports = AppStore;

由于 Facebook Flux 的圣经比例文档匮乏,我不知道我使用的是以前版本的代码。

AppDispatcher.js中,您需要使用new关键字按以下方式定义AppDispatcher:

var Dispatcher = require('flux').Dispatcher;
var assign = require('object-assign');

var AppDispatcher = assign(new Dispatcher(), {
  handleViewAction: function(action) {
    this.dispatch({
      source: 'VIEW_ACTION',
      action: action
    });
  }
});

module.exports = AppDispatcher;

这里是一个 link 到存储库的工作代码:https://github.com/bengrunfeld/react-flux-simple-app