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

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

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