【问题标题】:Durandal Routing not working--am I missing a configuration setting?Durandal 路由不起作用——我是否缺少配置设置?
【发布时间】:2013-04-30 12:18:33
【问题描述】:

我正在尝试设置 Durandal 应用程序,我从一个非常简单的配置开始,只有两个视图。

发生的情况是,当我调用 router.navigateTo('#/welcome') 或 router.navigateTo('#/primaryapplicants') 时,视图不会改变。但是,地址栏中的 URL 会发生变化,如果我重新加载页面,我会看到我试图导航到的视图。

我做错了什么?

这是video demonstrating the behavior I'm experiencing。我在视频中引用的代码如下。

main.js

require.config({
    paths: { "text": "durandal/amd/text" }
});

define(function (require) {
    var system = require('durandal/system'),
        app = require('durandal/app'),
        router = require('durandal/plugins/router'),
        viewLocator = require('durandal/viewLocator'),
        logger = require('services/logger');

    system.debug(true);

    app.start().then(function () {
        // route will use conventions for modules
        // assuming viewmodels/views folder structure
        router.useConvention();

        // When finding a module, replace the viewmodel string 
        // with view to find it partner view.
        // [viewmodel]s/sessions --> [view]s/sessions.html
        // Otherwise you can pass paths for modules, views, partials
        // Defaults to viewmodels/views/views. 
        viewLocator.useConvention();

        // Will assume that a URL to #/something corresponds to a viewmodels/something viewmodel.
        // http://durandaljs.com/documentation/Router/)
        //router.mapAuto();

        var routes = [{
            url: 'welcome',
            moduleId: 'viewmodels/welcome',
            name: 'Welcome',
            visible: true
        }, {
            url: 'primaryapplicants',
            moduleId: 'viewmodels/primaryapplicants',
            name: 'Primary Applicants',
            visible: true
        }];

        router.map(routes);

        app.setRoot('viewmodels/shell');

        // override bad route behavior to write to 
        // console log and show error toast.
        // This method also accepts a "params" parameters, but we're not using it,
        // and to avoid JSLint warnings, we're not including it.
        router.handleInvalidRoute = function (route) {
            logger.logError('No route found', route, 'main', true);
        };
    });
});

shell.html

<div>
Shell HTML
<ul id="navbar">
    <li><a href="#/welcome">Welcome</a></li>
    <li><a href="#/primaryapplicants">Primary Applicants</a></li>
</ul>

        <!--ko compose: {model: router.activeItem} -->
        <!--/ko-->
</div>

shell.js

define(['durandal/system', 'services/logger', 'durandal/plugins/router'],
    function (system, logger, router) {


        function activate() {

            // Listen for click events so we can navigate
            $('body').on('click', '#navbar a', function (event) {
                var navTo = '#/welcome';
                navTo = event.target.hash;
                logger.log('Attempting navigation to ' + navTo,
                        null,
                        system.getModuleId(shell),//ignore jslint
                        true);
                router.navigateTo(navTo);
                //alert("About to navigate to " + event.target.hash);
                //router.navigateTo(event.target.hash);
            });


            logger.log('Shell loaded',
                null,
                system.getModuleId(shell),//ignore jslint
                true);

            return router.activate('welcome');
        }

        var shell = {
            activate: activate,
            router: router
        };

        return shell;

    }
);

【问题讨论】:

    标签: javascript url-routing single-page-application durandal


    【解决方案1】:

    通常情况下,一旦你找出解决方案,它真的很简单。

    显然,在 compose 绑定(Durandal 提供的自定义 Knockout 绑定)中,您必须具有 afterCompose 绑定。换句话说,这是:

        <!--ko compose: {model: router.activeItem} -->
        <!--/ko-->
    

    需要改成这样:

        <!--ko compose: {model: router.activeItem, afterCompose: router.afterCompose} -->
        <!--/ko-->
    

    没有它,我的导航将无法工作。这是stated clearly enough in the docs,但我还是以某种方式错过了它:

    您必须将 modelafterCompose 回调都连接到 路由器,以便一切正常工作。

    【讨论】:

    • 有时只需阅读文档即可。从那时起,一切都是为了让它下沉,至少对我来说是这样的;-)。
    • @Josh - 我想你有点误解了 - compose 绑定属于 Durandal 本身(作为自定义 KO 绑定),在正常情况下它实际上不需要 afterCompose 回调,只是为了shell 页面组成。
    • 好的,所以这是 Durandal 的东西,而不是 Knockout 的原生部分。但我不明白你的意思是在正常情况下不需要 afterCompose 。 Durandal 文档说是的。你能进一步解释一下你的意思吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    相关资源
    最近更新 更多