我正在使用助焊劑作爲模式來構建應用程序。 我想添加一個加載器(微調)時,進行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都還未被登記爲調度回調。 如何確保在任何操作被觸發之前它已被註冊?