2016-03-01 34 views
0

我正在使用助焊剂作为模式来构建应用程序。 我想添加一个加载器(微调)时,进行API调用,但它不工作,我想我失去了一些东西。 流程是这样的:当 应用程序加载我打电话initApp助焊剂 - 在店面注册之前发送的动作

var InitActions = { 
initApp: function(){ 
    FooActions.getFoo(); 
}}; 
module.exports = InitActions; 

FooActions调度GET_FOO行动,并呼吁以APIService.foo.getFooByUser

var FooActions = { 

getFoo: function(){ 
    var account = accountStore.getAccountData(); 
    var accountId = account.id; 
    Dispatcher.dispatch({ 
     actionType: ActionTypes.GET_FOO 
    }); 
    APIService.foo.getFooByUser(accountId); 
}}; 
module.exports = FooActions; 

APIService将使一个Ajax调用和在响应将触发ServerActions.getFooSuccess或ServerActions.getFooFailed操作

var APIService = { 
    foo: { 
     getFooByUser : function(accountId){ 

     var url = apiUrl; 
     var params = { 
      "accountId":accountId 
     }; 
     promise.post(url,params) 
      .then(function(response){ 
       ServerActions.getFooSuccess(response); 
      }) 
      .catch(function(response){ 
       ServerActions.getFooFailed(response); 
      }); 
     } 
    } 
}; 
module.exports = APIService; 

ServerActions wi LL调度GET_FOO_SUCCESS或GET_FOO_Failed

var ServerActions = { 
getFooSuccess: function(response){ 
    Dispatcher.dispatch({ 
     actionType: ActionTypes.GET_FOO_SUCCESS, 
     foo: response 
    }); 
}, 

getFooFailed: function(response){ 
    Dispatcher.dispatch({ 
     actionType: ActionTypes.GET_FOO_FAILED 
    }); 
} 
} 

和foo的商店通过dispatcher.register

var FooStore = assign({}, EventEmitter.prototype,{...}; 
Dispatcher.register(function(action){ 
switch (action.actionType){ 
    case ActionTypes.GET_FOO: 
     _isLoading = true; 
     FooStore .emitChange(); 
     break; 
    case ActionTypes.GET_FOO_SUCCESS: 
     _isLoading = false; 
     _foo = action.foo; 
     FooStore .emitChange(); 
     break; 
    case ActionTypes.GET_FOO_FAILED: 
     _isLoading = false; 
     FooStore.emitChange(); 
     break; 
    default: 
    // do nothing 
}}); 

听着这些事件现在的基础上,_isLoading PARAM我知道什么时候来显示和隐藏装载机在我FOO零件。由于某些原因,代码从未进入GET_FOO的情况,尽管此操作在API调用之前分派。 有人能告诉我为什么吗?

编辑: 当我调试调度员的代码,我可以在调度功能看到循环

Dispatcher.prototype.dispatch = function dispatch(payload) { 
    !!this._isDispatching ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Dispatch.dispatch(...): Cannot dispatch in the middle of a dispatch.') : invariant(false) : undefined; 
    this._startDispatching(payload); 
    try { 
     for (var id in this._callbacks) { 
     if (this._isPending[id]) { 
      continue; 
     } 
     this._invokeCallback(id); 
     } 
    } finally { 
     this._stopDispatching(); 
    } 
    }; 

我能看到的FooStore都还未被登记为调度回调。 如何确保在任何操作被触发之前它已被注册?

回答

0

解决: 我正在做确保FooStore被任何阳离子之前注册的要求它我打电话给initApp()