提交 719770f4 authored 作者: vipcxj's avatar vipcxj

增加模块页面跳转的钩子

上级 0b64c728
......@@ -75,6 +75,33 @@ const createRoute = async (app, group, basePath) => {
};
};
const moduleHook = (app, uid, lastModule, currentModule) => {
// noinspection JSUnresolvedVariable
const store = app._store;
if (store) {
if (lastModule && lastModule.model && (lastModule.model.reducerExitHook || lastModule.model.effectExitHook)) {
store.dispatch({
type: `${lastModule.model.namespace}/@@exit`,
data: {
from: lastModule,
to: currentModule,
},
});
}
if (currentModule && currentModule.model && (currentModule.model.reducerEnterHook || currentModule.model.effectEnterHook)) {
store.dispatch({
type: `${currentModule.model.namespace}/@@enter`,
data: {
from: lastModule,
to: currentModule,
},
});
}
}
if (currentModule) {
histories.pushHistory('module', uid, currentModule);
}
};
const createRoutes = async (app, modules, groups, basePath) => {
const routes = [];
......@@ -95,8 +122,9 @@ const createRoutes = async (app, modules, groups, basePath) => {
fullPath: combinePath(basePath, name),
name: showName,
};
let modelBundle;
if (layout.route) {
const modelBundle = await import(`./models/main/modules/${layout.route}`);
modelBundle = await import(`./models/main/modules/${layout.route}`);
registerModel(app, modelBundle.default);
let routeBundle = await import(`./routes/main/modules/${layout.route}`);
routeBundle = routeBundle.default || routeBundle;
......@@ -113,19 +141,33 @@ const createRoutes = async (app, modules, groups, basePath) => {
} else {
route.component = Monk;
}
let model;
if (modelBundle) {
model = {};
model.namespace = modelBundle.namespace;
model.reducerEnterHook = !!model.reducers['@@enter'];
model.effectEnterHook = !!model.effects['@@enter'];
model.reducerExitHook = !!model.reducers['@@exit'];
model.effectExitHook = !!model.effects['@@exit'];
}
if (route.onEnter) {
const onEnter = route.onEnter;
route.onEnter = (nextState, replace, cb) => {
if (get(nextState, 'location.pathname') === route.fullPath) {
getUser()
.then(u => u && u.id)
.then(uid => histories.pushHistory('module', uid, {
name,
showName,
icon,
description,
path: route.fullPath,
}))
.then((uid) => {
const lastModule = histories.getLatest('module', uid);
const currentModule = {
name,
showName,
icon,
description,
path: route.fullPath,
model,
};
moduleHook(app, uid, lastModule, currentModule);
})
.then(() => new Promise((resolve, reject) => {
onEnter(nextState, replace, (err, res) => {
if (err) {
......@@ -146,13 +188,18 @@ const createRoutes = async (app, modules, groups, basePath) => {
if (get(nextState, 'location.pathname') === route.fullPath) {
getUser()
.then(u => u && u.id)
.then(uid => histories.pushHistory('module', uid, {
name,
showName,
icon,
description,
path: route.fullPath,
}))
.then((uid) => {
const lastModule = histories.getLatest('module', uid);
const currentModule = {
name,
showName,
icon,
description,
path: route.fullPath,
model,
};
moduleHook(app, uid, lastModule, currentModule);
})
.then(() => cb())
.catch(err => cb(err));
} else {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论