1
當服務器發出401響應時,我通過請求新的身份驗證令牌來處理錯誤,之後我需要繼續實際發生的請求。因此,我無法訪問作爲範圍的類方法,即在響應中未定義它。嘗試自己發送操作
;(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);
})();
有沒有關於這種情況的任何其他選擇,我非常願意傾聽。