【问题标题】:Durandal Child router re-activate all level up to rootDurandal子路由器重新激活所有级别,直到root
【发布时间】:2015-02-19 23:33:13
【问题描述】:

我们正在使用durandaljs 开发一个应用程序,我们似乎遇到了子路由器的问题,或者至少是我们不理解或无法弄清楚的问题。让我们从一个例子开始。

Route => /part1/:id/part2/:id2/part3

问题,main.jsactivate 方法在导航时在每个级别的路由上调用:

= Root (activate called)
== Part1 (activate called)
=== Part 2 (activate called)
==== Part 3 (activate called (normal since this is the initiator of everything else since that's where we have navigating to, no more child router declare here as it live in the child router declare in Part2))

我们发现这种行为有点奇怪,想知道这是正常行为还是我们在子路由器上做错了什么。为什么每个子级别都需要重新运行他们的激活方法? IHMO,只有我们导航到的当前模型应该触发他的激活方法,并且任何其他子页面都不应该触发这个事件。

我们正在使用最新的DurandalJS,即2.1.0,并且我们正在使用Typescript 构建应用程序,尽管我认为它不会在这里改变什么。

【问题讨论】:

    标签: durandal durandal-navigation


    【解决方案1】:

    这是正常行为。每当激活子级时,激活函数就会在整个层次结构中传播。这 not 与停用行为匹配。在父级仍在实例化时停用子级不会调用父级上的停用。

    处理此问题的最佳方法是打开激活器回调的参数,因为激活参数会传递给每个激活器回调。因此,对于第 1 部分,您可能有

    activate(id1, id2) {
        if (id1 === null && id2 === null) {
            // logic
        }
    }
    

    这错过了您导航到/part1//part2 的边缘情况,因此如果您担心这一点,您可以在根路由器上使用正则表达式进行更可靠的检查。

    import router = require('plugins/router');
    activate(id1, id2) {
        var route = /^part1\/?$/;
        if (route.test(router.activeInstruction)) {
            // logic
        }
    }
    

    【讨论】:

    • 感谢您确认我的想法!
    猜你喜欢
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 2015-12-19
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多