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

增加模块页面跳转的钩子

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