反应Dispatcher.dispatch,而不是将有效负载调度到存储

时间:2016-07-29 04:45:34

标签: reactjs flux reactjs-flux

我在“action”中设置了有效负载,目的是将api数据发送到商店。但是在Dispatcher.register中没有触发switch case。

Flux版本:“flux”:“^ 2.1.1”,

1)动作文件:(注意:已经确认使用调试器触发了receivedAllServices)

"use strict"

var Dispatcher = require('../dispatcher/appDispatcher');
// var requestActions = require('./requestActions');
var ActionTypes = require('../constants/actionTypes');

var ResponseActions = {

    receivedAllServices: function(all_services){

        console.log('response received');
        debugger;

        Dispatcher.dispatch({
            actionType: ActionTypes.RECEIVED_ALL_SERVICES,
            services: all_servicess
        });
    }



};

module.exports = ResponseActions;

2)存储:(注意:不会触发存储操作中的调试器)

Dispatcher.register(function(action){
    switch(action.actionType){
        case ActionTypes.RECEIVED_ALL_SERVICES:

            debugger;

            // AuthorStore.emitChange();
            break;
    }
});

3)调度程序文件:

var Dispatcher = require('flux').Dispatcher;

module.exports = new Dispatcher();

4)actionTypes.js文件

"use strict"

var keyMirror = require('fbjs/lib/keyMirror');

module.exports = keyMirror({
    RECEIVED_ALL_SERVICES: null, 
});

1 个答案:

答案 0 :(得分:0)

嗯,这很奇怪。一切看起来都很正确。您能否检查Dispatcherstore.js中的action.js是否是同一个对象?

请参阅以下具有相同操作/存储的简短示例。一切正常(按run并查看控制台输出)。

var appDispatcher = new Flux.Dispatcher();

// This code should be our action
function receivedAllServices() {
  appDispatcher.dispatch({
    actionType: 'RECEIVED_ALL_SERVICES',
    services: ['serv1','serv2'],
  });
  console.log('Done');
}


// This code should be our store
appDispatcher.register(action => {
  console.log('action', action);
  switch(action.actionType){
    case 'RECEIVED_ALL_SERVICES':
      //debugger;
      // AuthorStore.emitChange();
      console.log('AuthorStore.emitChange');
      break;
  }
});

// Call the action
receivedAllServices();
<script src="https://cdnjs.cloudflare.com/ajax/libs/flux/2.1.1/Flux.js"></script>