2016-08-23 51 views
2

我已经创建了一个基本的授权流程,使用了redux,redux-saga和不可变的js。redux传奇和immutablejs

Redux窗体(v6.0.0-rc.4)允许窗体创建一个不可变的地图。我将这些值传递给redux-saga,我试图将这些值传递给我的登录函数。

问题1:从概念上讲,什么时候可以使用values.get('username')访问不可变映射中的数据?在我的传奇中,在功能?我是否应该等到最后一步提取值?

问题2:假设我能够在正确的地点,以提取值,我不知道我怎么看这应该在传奇中进行处理 - 这是我loginFlow传奇:

export function* loginFlow(data) { 
    while (true) { 
    yield take(LOGIN_REQUEST); 

    const winner = yield race({ 
     auth: call(authorize, { data, isRegistering: false }), 
     logout: take(LOGOUT), 
    }); 

    if (winner.auth) { 
     yield put({ type: SET_AUTH, newAuthState: true }); 
     forwardTo('/account'); 
    } else if (winner.logout) { 
     yield put({ type: SET_AUTH, newAuthState: false }); 
     yield call(logout); 
     forwardTo('/'); 
    } 

    } 
} 

data是从redux形式不可变的映射。然而,无论何时我在我的传奇中登录日志data,它只会返回0

回答

1

显然我并没有经过处理的不可变映射到正确的动作 - 正确的代码:

export function* loginFlow() { 

    while (true) { 

    // this line ensures that the payload from the action 
    // is correctly passed through the saga 

    const { data } = yield take(LOGIN_REQUEST); 

    const winner = yield race({ 

     // this line passes the payload to the login/auth action 

     auth: call(authorize, { data, isRegistering: false }), 
     logout: take(LOGOUT), 
    }); 

    if (winner.auth) { 
     yield put({ type: SET_AUTH, newAuthState: true }); 
     forwardTo('/account'); 
    } else if (winner.logout) { 
     yield put({ type: SET_AUTH, newAuthState: false }); 
     yield call(logout); 
     forwardTo('/'); 
    } 
    } 
}