2016-03-28 70 views
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); 
    })(); 

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

回答

1

您可以只用

dispatch(someAction.addItem) 
派遣同样的动作
相关问题