【发布时间】:2013-10-29 07:17:08
【问题描述】:
我正在构建一个应用程序,并希望显示一个 2 层菜单,这两个层级始终可用。 Durandal 2.0 introduced their new router,支持“子路由器”,可以更轻松地进行深度链接。
我的问题 - 我可以永久加载我的“子”导航路线(并在父不活动时呈现子菜单),还是“子路由器”设计旨在懒惰 -一旦要评估深层链接,就加载它们以评估它们?
Durandal 示例显示主导航注册一个 splat 路由,然后当该视图模型被加载/激活时,子路由被注册。
例如:在 Durandal 2.0 提供的示例中,mian nev 注册在shell.js
router.map([
{ route: ['', 'home'], moduleId: 'hello/index', title: 'Validation test', nav: true },
{ route: 'knockout-samples*details', moduleId: 'ko/index', title: 'Knockout Samples', nav: true, hash: '#knockout-samples' }
]).buildNavigationModel()
.activate();
然后ko/index.js 视图模型注册孩子(在activate())
var childRouter = router.createChildRouter()
.makeRelative({
moduleId:'ko',
fromParent:true
}).map([
{ route: '', moduleId: 'helloWorld/index', title: 'Hello World', type: 'intro' },
{ route: 'helloWorld', moduleId: 'helloWorld/index', title: 'Hello World', type: 'intro', nav: true},
{ route: 'clickCounter', moduleId: 'clickCounter/index', title: 'Click Counter', type: 'intro', nav: true}
]).buildNavigationModel();
而我希望在一个地方定义我的路线,例如:
var routes = [
{ route: ['', 'home'],
moduleId: 'hello/index',
title: 'Validation test',
nav: true },
{ route: 'knockout-samples*details',
moduleId: 'ko/index',
moduleRootId: 'ko', // Custom property to make child routes easier
title: 'Knockout Samples',
nav: true,
hash: '#knockout-samples',
childRoutes: [
{ route: '', moduleId: 'helloWorld/index', title: 'Hello World', type: 'intro' },
{ route: 'helloWorld', moduleId: 'helloWorld/index', title: 'Hello World', type: 'intro', nav: true},
{ route: 'clickCounter', moduleId: 'clickCounter/index', title: 'Click Counter', type: 'intro', nav: true}
]}
])
我正在努力的部分是注册子路线,并在导航到它们时让它们正确评估。 (渲染菜单将在稍后进行......)
// Load routes router.map(routes.navRoutes).buildNavigationModel().mapUnknownRoutes(function(instruction)
{
logger.logError('No Route Found', instruction.fragment, 'main', true);
});
// Try load child routes...
$.each(routes.navRoutes, function(index, parentRoute) {
if (!parentRoute.childRoutes) {
return;
}
var childRouter = router.createChildRouter()
.makeRelative({
moduleId: parentRoute.moduleRootId,
fromParent: true // also tried false + non-splat routes...
}).map(parentRoute.childRoutes).buildNavigationModel();
});
我在渲染此导航时遇到了各种错误。尝试计算哈希(来自未激活的父级或子级)时,主要是内部 router.js 错误 - 因此所有哈希都已定义。
一旦我用它来映射导航,子路线似乎无法访问 - 他们只是加载主 splat 页面而没有错误。
所以我想知道我是否以正确的方式处理这件事? (预先注册所有子路由,目标是渲染一个 2 层菜单)。
我认为后备方案是使用 Durandal 1.2 answer about subrouting 之类的东西,使用平面路由注册、自定义“isSameItem”函数和计算的 observables 的组合来呈现 2 层导航。
【问题讨论】:
标签: durandal hottowel durandal-navigation