【问题标题】:Durandal: At what stage in lifecycle should router.isNavigating deactivate?Durandal:router.isNavigating 应该在生命周期的哪个阶段停用?
【发布时间】:2013-08-28 11:11:11
【问题描述】:

叹息这已经成为我的“日常问题”例行公事了,对不起!

我有一个 nav.html,其中包含:

    <div class="loader pull-right" data-bind="css: { active: router.isNavigating }">
        <i class="icon-spinner icon-2x icon-spin"></i>
    </div>

问题是,它永远不会停用!基本上,微调器始终保持旋转。

我已经在视图模型中进行了调试,就像在 Durandal html 示例页面中一样:

    var vm = {
        activate: activate,
        //refresh: refresh,
        //sessions: sessions,
        title: 'My App Home Page',
        activate: function () {
            system.log('Lifecycle : activate : home');
        },
        binding: function () {
            system.log('Lifecycle : binding : home');
            return { cacheViews: false }; //cancels view caching for this module, allowing the triggering of the detached callback
        },
        bindingComplete: function () {
            system.log('Lifecycle : bindingComplete : home');
        },
        attached: function (view, parent) {
            system.log('Lifecycle : attached : home');
        },
        compositionComplete: function (view) {
            system.log('Lifecycle : compositionComplete : home');
        },
        detached: function (view) {
            system.log('Lifecycle : detached : home');
        }

        //viewAttached: viewAttached
    };

在我的 Chrome 开发控制台中,我有:

Lifecycle : bindingComplete : home system.js:73
Lifecycle : attached : home system.js:73
Lifecycle : compositionComplete : home system.js:73

...我会认为“compositionComplete”会触发“router.isNavigating”的结束,不是吗?

nav.html 在我的 shell.html 文件中是这样组成的:

<header>
    <!-- ko compose: {view: 'nav'} -->
    <!-- /ko-->
</header>

基本上,微调器只是停留在右上角,永远不会消失。我可以在我的两个页面之间导航,并且它只是停留在那里,即使每次都会触发“compositionComplete”。

整个 shell.html 文件:

<div>
    <header>
        <!-- ko compose: {view: 'nav'} -->
        <!-- /ko-->
    </header>
      <div>
        <section id="content" class="main">
            <!--ko router: { transition:'entrance', cacheViews:true }--><!--/ko-->
        </section>
      </div>
</div>

编辑的 shell.html 文件:

<div>
    <header>
        <nav class="navbar navbar-default">
          <div class="navbar-header">
                <a class="navbar-brand" href="/">
                    <span class="title">My test SPA</span>
                </a>
            </div>
            <div>
                <ul class="nav navbar-nav" data-bind="foreach: router.navigationModel">
                    <li data-bind="css: { active: isActive }">
                        <a data-bind="attr: { href: hash }, html: title"></a>
                    </li>
                </ul>
                <p class="navbar-text pull-right">Logged in as <a href="#" class="navbar-link">Bilbo Baggins</a></p>
                <div class="loader pull-right" data-bind="css: { active: router.isNavigating }">
                    <i class="icon-spinner icon-2x icon-spin"></i>
                </div>

            </div>
        </nav>
    </header>
      <div>
        <section id="content" class="main">
            <!--ko router: { transition:'entrance', cacheViews:true }--><!--/ko-->
        </section>
      </div>
</div>

【问题讨论】:

    标签: durandal


    【解决方案1】:

    这可能是因为您在shell.html 中使用router 进行合成的方式。

    在 durandal 2.0 中应该如下所示:

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

    或者,如果您想像旧方式一样保留它:

    <!--ko compose: {model: router.activeItem, 
            attached: router.attached,      
            compositionComplete: router.compositionComplete,     
            transition: 'entrance'} -->
        <!--/ko-->
    

    还要考虑到 css 类 active 可能无法在加载 gif 的 div 中按预期工作。相反,您可以使用:

    <div class="loader pull-right" data-bind="visible: router.isNavigating">
    

    所以你会在加载器 gif 不导航时隐藏它。

    【讨论】:

    • 当我找到有关“新”方法的文档时,我也是这么想的。但是我今天早上已经编辑了(见上文,因为我已经编辑了初始帖子以显示整个 shell.html 文件)。没有区别。
    • @CarlEdwards 很酷,谢谢。顺便问一下,你用的是子路由器吗?
    • @CarlEdwards 也许问题是在 shell 中编写 nav 视图存在问题。您可以尝试将nav 中的代码直接粘贴到shell 中,看看会发生什么。我怀疑问题可能在那里..
    • 不知道什么是子路由,抱歉!已编辑 op 以显示已编辑的 shell.html 文件。没有变化。
    • @CarlEdwards 你能不能把绑定改成router.isNavigating 这个:&lt;div class="loader pull-right" data-bind="visible: router.isNavigating"&gt;
    猜你喜欢
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 2017-10-06
    • 1970-01-01
    • 2017-11-10
    • 2013-05-05
    • 2016-08-05
    相关资源
    最近更新 更多