美文网首页
models的数据请求时机

models的数据请求时机

作者: 奈文摩尔定律 | 来源:发表于2017-05-17 10:58 被阅读28次
    import * as userService from '../services/userresult';
    
    const methods = {
      user: userService.fetch,
      orgs: userService.fetchorgs,
      repos: userService.fetchrepos,
    };
    
    export default {
      namespace: 'userresult',
      state: {
        user: {},
        orgs: [],
        repos: [],
      },
      reducers: {
        saveUser(state, { payload: { data: user } }) {
          return { ...state, user };
        },
        saveOrgs(state, { payload: { data: orgs } }) {
          return { ...state, orgs };
        },
        saveRepos(state, { payload: { data: repos } }) {
          return { ...state, repos };
        },
      },
      effects: {
    
        // 查询用户资料
        // 机构
        // repos
        *fetch({ payload: { username, datakey = 'user' } }, { call, put }) {
          const { data } = yield call(methods[datakey], { username });
          yield put({
            type: 'save',
            payload: {
              data,
            },
          });
        },
      },
    

    在路由监听时候做触发的时机

      subscriptions: {
        // 监听路由,决定model的数据请求时机
        setup({ dispatch, history }) {
          return history.listen(({ pathname, query }) => {
            if (pathname === '/gitsearch/user/result') {
              const username = query.query;
              dispatch({ type: 'fetch', payload: { username } });
              dispatch({ type: 'fetch', payload: { username, datakey: 'orgs' } });
              dispatch({ type: 'fetch', payload: { username, datakey: 'repos' } });
            }
          });
        },
      },
    };
    

    相关文章

      网友评论

          本文标题:models的数据请求时机

          本文链接:https://www.haomeiwen.com/subject/kgqdxxtx.html