Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何正确的清空某个路由下model中的数据 #1194

Closed
henry-fun opened this issue Sep 8, 2017 · 3 comments
Closed

如何正确的清空某个路由下model中的数据 #1194

henry-fun opened this issue Sep 8, 2017 · 3 comments

Comments

@henry-fun
Copy link

我现在在写一个编辑新闻的页面,因为编辑和新建都是一样的逻辑,所以我写在同一个页面中的,但问题是当我进入编辑逻辑时model中会残留着该次新闻编辑的model数据,当我下次以新建新闻的逻辑进来时就会把上次编辑新闻时的残留model数据加载进来,从而会影响到页面,我现在清空model的做法是在effects中写一个clearModel方法:

*clearModel({ payload }, { put }) {
    yield put({
        type: 'clear',
    });
},

同时在reducer中新建一个clear方法:

clear() {
    return {};
},

然后在组件的componentWillUnMount中dispatch:

dispatch({
    type: 'model/clearModel',
    payload: {}
});

问下各位大佬,我这种写法有问题不?感觉这么写有点不妥啊。

@jdwdw
Copy link

jdwdw commented Sep 8, 2017

@hanxiansen
我之前用过的一种写法是在 model 中的 subscriptions 用 history.listen((location))去判断当前进入的路由去调用不同的方法,如果是编辑的话先从服务端获取数据再渲染,如果是创建的话,就把当前的status中的数据清空,(只是我自己的做法不知到是否合适)代码如下:

..........
  state: {
    topicData: {},
    topicId: '',
  },

  subscriptions: {

    setup ({ dispatch, history }) {
      history.listen((location) => {
        const match = pathToRegexp('/topic/:id/edit').exec(location.pathname)
        if (match) {
          dispatch({
            type: 'getTopic',
            payload: { id: match[1] },
          })
        } else {
          dispatch({
            type: 'updateState',
            payload: { topicData: {}, topicId: '' },
          })
        }
      })
    },

  },

..........
  effects: {
    * getTopic ({ payload }, { call, put }) {
      const accesstoken = localStorage.getItem('accesstoken')
      let params = { accesstoken }
      params = Object.assign(params, payload)
      let data = yield call(getTopic, params)
      console.log(data)
      if (data.success) {
        data = { topicData: data.data, topicId: data.data.id }
        // yield put({
        //   type: 'setTopicId',
        //   payload: { topicId: payload.id },
        // })
        yield put({ type: 'updateState',
          payload: data,
        })
      }
    },
.......
},

  reducers: {
     ........
    updateState (state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },

  },

@sorrycc
Copy link
Member

sorrycc commented Sep 8, 2017

你这么做没啥问题,state 的变动肯定得走 reducer 。不过编辑、新增的逻辑走 modal 的话,数据可以不放 model 里,参考 https://github.com/dvajs/dva/blob/master/packages/dva-example-user-dashboard/ 的处理方式。

@sorrycc sorrycc closed this as completed Sep 8, 2017
@ybning
Copy link

ybning commented Jul 31, 2018

@sorrycc 发的链接404了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants