1

我刚刚将我的项目升级到 durandaljs 2.0。从某种意义上说,如果我在浏览器地址栏中输入路由 url 并按回车,一切似乎都工作正常,它会很好地加载散列路由的视图。这适用于我的所有路线。

但是,如果我单击在 href 中具有完全相同的哈希路由的导航栏按钮,浏览器 url 会更改为正确的 url(如果我在地址栏中按 enter 就可以工作)但视图不会加载。

这几乎就像一个没有触发器的 router.navigate 被调用。

我实际上手动尝试调用 route.navigate("#xyz", { replace: false, trigger: true }) 但这也不会加载 xyz 视图。

我在这里忽略了一些愚蠢的事情-有什么想法吗?

来自 shell.js:

activate: function () {
    $.getJSON("/Account/GetCurrentUser").done(function (data, textStatus, jqXHR) {
        shell.user(data);
    });


    $(document).ajaxError(function (xhr, props) {
        if (props.status === 401) {
            window.location = "/Account/Login";
        }
    });

    router.map([
        { route: '', title: 'Jobs', moduleId: 'viewmodels/jobs', nav: true },
        { route: 'companies', title: 'Companies', moduleId: 'viewmodels/companies', nav: true },
        { route: 'profile', title: 'Profile', moduleId: 'viewmodels/profile', nav: true },
        { route: 'users', title: 'Users', moduleId: 'viewmodels/users', nav: true, hash: '#users' },
        { route: 'jobs(/filter/:filter)(/group/:group)(/sort/:sort)(/page/:page)', title: 'Jobs', moduleId: 'viewmodels/jobs', nav: false },
        { route: 'companies(/filter/:filter)(/group/:group)(/sort/:sort)(/page/:page)', title: 'Companies', moduleId: 'viewmodels/companies', nav: false }

        //,{ route: 'flickr', moduleId: 'viewmodels/flickr', nav: true }
    ]).buildNavigationModel();

    //router.mapNav('details');
    //router.mapNav('airports');
    //router.mapNav('emptylegs');

    return router.activate();
}

来自 nav.html:

<nav class="navbar navbar-fixed-top" style="max-height:68px">
    <div class="navbar-inner">
        <a class="brand" href="/">
            <span class="title"></span>
        </a>        
        <div>
            <ul class="nav" data-bind="foreach: router.navigationModel">
                <li><a data-bind="css: { active: isActive }, attr: { href: hash }, text: title"
                    class="btn btn-info" href="#"></a>
                </li>
            </ul>
        </div>
    </div>
</nav>

谢谢

4

1 回答 1

1

我不知道这是否是问题所在,但在a绑定中,您正在通过敲除更改 href 然后href="#"再次设置。

<a data-bind="css: { active: isActive }, attr: { href: hash }, text: title"
                class="btn btn-info" href="#"></a>

可能不是问题的原因。但这看起来不太好。。

顺便说一下,还要检查与路由器的绑定是否正确完成。在 Durandal 2.0 中它发生了变化,所以在你的shell.html你应该有这样的东西:

<!--ko router: { transition:'entrance', cacheViews:true }--><!--/ko-->

而不是这个:

<!--ko compose: {
    model: router.activeItem, 
    afterCompose: router.afterCompose, 
    transition: 'entrance'
} -->
于 2013-08-27T17:17:36.933 回答