2

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

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

4

1 回答 1

2

您可以通过发送相同的操作

dispatch(someAction.addItem)
于 2016-03-29T03:32:39.943 回答