试图自己发送一个动作

时间:2016-03-28 10:45:31

标签: javascript reactjs redux react-redux

当服务器抛出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);
    })();

关于这种情况还有其他选择,我非常愿意倾听。

1 个答案:

答案 0 :(得分:1)

您可以通过

分派相同的操作
dispatch(someAction.addItem)