我正在做一个 Hottowel 项目,我更新到了 Durandal 2.0。我遵循了从 1.x 到 2.0 的转换指南,但我仍然有一些问题:
1.加载router
插件总是超时,除非我像这样加载它:
app.configurePlugins({
dialog: true,
router: true
}, '../durandal/plugins');
注意路径参数。
2.我不能再从一个模块过渡到另一个模块。加载模块的唯一方法是使用 URL 中的特定哈希刷新页面。当我单击导航按钮时,浏览器 URL 中的哈希值会更改,但未加载模块。此外,router.isNavigating
卡在true
.
以下是文件:
main.js
require.config({
paths: {
'text': '../Scripts/text',
'durandal': '../Scripts/durandal',
'plugins': '../Scripts/durandal/plugins',
'transitions': '../Scripts/durandal/transitions',
'services' : 'services'
}
});
define('jquery', function () { return jQuery; });
define('knockout', ko);
define(['durandal/app', 'durandal/viewLocator', 'durandal/system', 'services/logger'],
function (app, viewLocator, system, logger)
system.debug(true);
app.title = 'MyApp';
app.configurePlugins({
dialog: true,
router: true
}, '../durandal/plugins');
app.start().then(function () {
viewLocator.useConvention();
app.setRoot('viewmodels/shell', 'entrance');
});
});
shell.js
define(['durandal/plugins/router', 'durandal/app', 'services/logger', 'services/authentication', 'config'],
function (router, app, logger, authentication, config) {
return {
router: router,
activate: function () {
router.map(config.routes)
.buildNavigationModel()
.mapUnknownRoutes('home');
logger.log('Application loaded', {}, 'shell', true);
return router.activate();
},
authentication: authentication
};
});
config.routes
[
{ route: ['', 'home'], moduleId: 'viewmodels/home', title: 'Home', nav: true, caption: '<i class="icon-home"></i> Home' },
{ route: 'mainMenu', moduleId: 'viewmodels/mainMenu', title: 'Main Menu', nav: true, caption: '<i class="icon-th-list"></i> Main Menu' },
{ route: 'logIn', moduleId: 'viewmodels/logIn', title: 'Log In', nav: false },
{ route: 'profile', moduleId: 'viewmodels/profile', title: 'Profile', nav: false },
{ route: 'admin', moduleId: 'viewmodels/admin', title: 'Admin', nav: false }
]
视图模型示例:viewmodels/logIn.js
define(['services/logger', 'durandal/plugins/router', 'services/authentication'],
function (logger, router, authentication) {
var userName = ko.observable(),
password = ko.observable(),
rememberMe = ko.observable();
function activate() {
logger.log('Log in view activated', null, 'logIn', true);
return true;
}
function logIn() {
authentication.logIn(userName(), password(), rememberMe());
}
var vm = {
activate: activate,
userName: userName,
password: password,
rememberMe: rememberMe,
logIn: logIn
};
return vm;
});
编辑
shell.html
<div>
<header>
<!--ko compose: {view: 'nav'} --><!--/ko-->
</header>
<section id="content" class="main container">
<!--ko compose: {
model: router.activeItem,
afterCompose: router.afterCompose,
transition: 'entrance'
} -->
<!--/ko-->
</section>
<footer>
<!--ko compose: {view: 'footer'} --><!--/ko-->
</footer>
</div>