试图自己派发一个动作
Trying to dispatch an action by itself
当服务器抛出 401 响应时,我通过请求新的身份验证令牌来处理错误,之后我需要继续执行实际需要发生的请求。因此,我无法访问 class 方法作为范围,即这在响应中未定义。
;(function(){
var someAction = {
addItem: function (entity, data) {
var that = this; /* this is undefined, out of scope */
return function (dispatch) {
dispatch(apiActions.apiRequest(entity));
return (ApiUtil.create(entity, data).then(function (response) {
dispatch(apiActions.apiResponse(entity));
browserHistory.goBack();
}, function (error) {
if (error.status == 401) {
ApiUtil.refreshSession().then(function (response) {
dispatch(that.addItem); /** unable to access that as i need to recall the same action after I get refresh token**/
});
} else {
dispatch(apiActions.apiResponse(entity));
}
}));
}
}
}
})();
;(function(){
/* within react component i've invoked this method */
this.props.actions.addItem('entity', some_entity);
var mapDispatchToProps = function (dispatch) {
return {
actions: bindActionCreators(_.assign({}, crudActions, apiActions), dispatch)
}
};
module.exports = connect(mapStateToProps, mapDispatchToProps)(SomeComponent);
})();
这个场景还有其他的选择吗,我很愿意听
您可以通过
发送相同的动作
dispatch(someAction.addItem)
当服务器抛出 401 响应时,我通过请求新的身份验证令牌来处理错误,之后我需要继续执行实际需要发生的请求。因此,我无法访问 class 方法作为范围,即这在响应中未定义。
;(function(){
var someAction = {
addItem: function (entity, data) {
var that = this; /* this is undefined, out of scope */
return function (dispatch) {
dispatch(apiActions.apiRequest(entity));
return (ApiUtil.create(entity, data).then(function (response) {
dispatch(apiActions.apiResponse(entity));
browserHistory.goBack();
}, function (error) {
if (error.status == 401) {
ApiUtil.refreshSession().then(function (response) {
dispatch(that.addItem); /** unable to access that as i need to recall the same action after I get refresh token**/
});
} else {
dispatch(apiActions.apiResponse(entity));
}
}));
}
}
}
})();
;(function(){
/* within react component i've invoked this method */
this.props.actions.addItem('entity', some_entity);
var mapDispatchToProps = function (dispatch) {
return {
actions: bindActionCreators(_.assign({}, crudActions, apiActions), dispatch)
}
};
module.exports = connect(mapStateToProps, mapDispatchToProps)(SomeComponent);
})();
这个场景还有其他的选择吗,我很愿意听
您可以通过
发送相同的动作dispatch(someAction.addItem)