提交 2cb70cf2 authored 作者: vipcxj's avatar vipcxj

不再在切换菜单时unmodel对应模块model

上级 73bfc856
......@@ -8,7 +8,6 @@ import persistStore from 'redux-persist/lib/persistStore';
import { PersistGate } from 'redux-persist/integration/react';
import { REHYDRATE } from 'redux-persist/lib/constants';
import isString from 'lodash/isString';
import get from 'lodash/get';
import config from './utils/config';
import { getUser, isAuthed, hasDomain, histories } from './utils/auth';
import { processError } from './utils/error';
......@@ -82,14 +81,11 @@ const moduleLeaveHook = (app, module) => {
const models = (app._models || []).filter(m => m.namespace.startsWith(`${module.name}/`));
const store = app._store;
models.forEach((m) => {
if (m.global) {
const { reducers, effects } = m;
if ((reducers && reducers['@@exit']) || (effects && effects['@@exit'])) {
store.dispatch({ type: `${m.namespace}/@@exit`, payload: module });
}
} else {
app.unmodel(m.namespace);
const { reducers, effects } = m;
if ((reducers && reducers['@@exit']) || (effects && effects['@@exit'])) {
store.dispatch({ type: `${m.namespace}/@@exit`, payload: module });
}
store.dispatch({ type: `${m.namespace}/@@reset` });
});
};
......@@ -97,11 +93,9 @@ const moduleEnterHook = (app, uid, module) => {
const models = (app._models || []).filter(m => m.namespace.startsWith(`${module.name}/`));
const store = app._store;
models.forEach((m) => {
if (m.global) {
const { reducers, effects } = m;
if ((reducers && reducers['@@enter']) || (effects && effects['@@enter'])) {
store.dispatch({ type: `${m.namespace}/@@enter`, payload: module });
}
const { reducers, effects } = m;
if ((reducers && reducers['@@enter']) || (effects && effects['@@enter'])) {
store.dispatch({ type: `${m.namespace}/@@enter`, payload: module });
}
});
histories.pushHistory('module', uid, module);
......@@ -126,7 +120,7 @@ const createRoutes = async (app, modules, groups, basePath) => {
fullPath: combinePath(basePath, name),
name: showName,
};
let modelBundle;
// let modelBundle;
if (layout.route) {
// modelBundle = await import(`./models/main/modules/${layout.route}`);
// modelBundle = modelBundle.default;
......@@ -151,15 +145,15 @@ const createRoutes = async (app, modules, groups, basePath) => {
} else {
route.component = Monk;
}
let model;
if (modelBundle) {
model = {};
model.namespace = modelBundle.namespace;
model.reducerEnterHook = modelBundle.reducers && !!modelBundle.reducers['@@enter'];
model.effectEnterHook = modelBundle.effects && !!modelBundle.effects['@@enter'];
model.reducerExitHook = modelBundle.reducers && !!modelBundle.reducers['@@exit'];
model.effectExitHook = modelBundle.effects && !!modelBundle.effects['@@exit'];
}
// let model;
// if (modelBundle) {
// model = {};
// model.namespace = modelBundle.namespace;
// model.reducerEnterHook = modelBundle.reducers && !!modelBundle.reducers['@@enter'];
// model.effectEnterHook = modelBundle.effects && !!modelBundle.effects['@@enter'];
// model.reducerExitHook = modelBundle.reducers && !!modelBundle.reducers['@@exit'];
// model.effectExitHook = modelBundle.effects && !!modelBundle.effects['@@exit'];
// }
const infoEx = {
...info,
path: route.fullPath,
......@@ -167,51 +161,39 @@ const createRoutes = async (app, modules, groups, basePath) => {
const { onLeave } = route;
if (onLeave) {
route.onLeave = (preState) => {
if (get(preState, 'location.pathname') === route.fullPath) {
moduleLeaveHook(app, infoEx);
}
moduleLeaveHook(app, infoEx);
onLeave(preState);
};
} else {
route.onLeave = (preState) => {
if (get(preState, 'location.pathname') === route.fullPath) {
moduleLeaveHook(app, infoEx);
}
route.onLeave = () => {
moduleLeaveHook(app, infoEx);
};
}
if (route.onEnter) {
const onEnter = route.onEnter;
route.onEnter = (nextState, replace, cb) => {
if (get(nextState, 'location.pathname') === route.fullPath) {
getUser()
.then(u => u.id)
.then(uid => moduleEnterHook(app, uid, infoEx))
.then(() => new Promise((resolve, reject) => {
onEnter(nextState, replace, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
}))
.then(() => cb())
.catch(err => cb(err));
} else {
return onEnter(nextState, replace, cb);
}
getUser()
.then(u => u.id)
.then(uid => moduleEnterHook(app, uid, infoEx))
.then(() => new Promise((resolve, reject) => {
onEnter(nextState, replace, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
}))
.then(() => cb())
.catch(err => cb(err));
};
} else {
route.onEnter = (nextState, replace, cb) => {
if (get(nextState, 'location.pathname') === route.fullPath) {
getUser()
.then(u => u.id)
.then(uid => moduleEnterHook(app, uid, infoEx))
.then(() => cb())
.catch(err => cb(err));
} else {
cb();
}
getUser()
.then(u => u.id)
.then(uid => moduleEnterHook(app, uid, infoEx))
.then(() => cb())
.catch(err => cb(err));
};
}
routes.push(route);
......
......@@ -5,9 +5,8 @@ import { shallowEqual } from './helper';
const registerModel = (app, model) => {
// noinspection JSUnresolvedVariable
if (app._models.filter(m => m.namespace === model.namespace).length === 1) {
if (model.global) {
return;
}
// eslint-disable-next-line no-console
console.warn(`model: ${model.namespace} exist! unmodel it and remodeled.`);
app.unmodel(model.namespace);
}
app.model(model);
......@@ -115,7 +114,9 @@ const hackSubscriptions = (module, subscriptions) => {
export const hackModel = (module, model) => {
model = { ...model };
model.namespace = `${module}/${model.namespace}`;
model.initialState = model.state;
model.initialState = { ...model.state };
model.reducers = model.reducers || {};
model.reducers['@@reset'] = () => model.initialState;
model.effects = hackEffects(module, model.effects || {});
model.subscriptions = hackSubscriptions(module, model.subscriptions || {});
return model;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论