2015-10-20 59 views
2

如果我有API調用一個異步操作,該操作既可以是一個動作返回的函數:如何撤消Redux異步操作? (多個步驟回到狀態)

export function asyncAction(itemId) { 
    return (dispatch) => { 
    dispatch(requestStarted()); 

    return sendRequest(itemId).then(
     (result) => dispatch(requestSuccess()), 
     (error) => dispatch(requestFail()) 
    ); 
    }; 
} 

或一個返回一個對象,並使用中間件攔截,並做東東:

export function asyncAction(itemId) { 
    return { 
    type: [ITEM_REQUEST, ITEM_REQUEST_SUCCESS, ITEM_REQUEST_FAILURE], 
    promise: sendRequest(itemId), 
    userId 
    }; 
} 

// same middleware found in https://github.com/rackt/redux/issues/99 
export default function promiseMiddleware() { 
    return (next) => (action) => { 
    const { promise, ...rest } = action; 
    if (!promise) { 
     return next(action); 
    } 

    next({ ...rest, readyState: 'request'); 
    return promise.then(
     (result) => next({ ...rest, result, readyState: 'success' }), 
     (error) => next({ ...rest, error, readyState: 'failure' }) 
    ); 
    }; 
} 

現在我的問題是:我如何回滾給國家asyncAction被派遣之前,這基本上意味着兩步找回狀態(成功/失敗=>請求)W/api調用撤消l快速通話。
例如,在刪除待辦事項(這是一個異步操作)後,彈出式快餐欄將顯示一個撤消選項,點擊後,刪除的待辦事項將添加回UI並添加一個api調用回到db。

我試過redux-undo但我覺得它並不打算解決這樣的問題。
或者我應該忘記'撤銷',只是發送一個全新的addTodo用戶單擊撤消選項時的操作?

在此先感謝:-)

回答