Durandal:多条路线,一个视图模型/视图 [英] Durandal: Multiple Routes, One ViewModel/View

查看:70
本文介绍了Durandal:多条路线,一个视图模型/视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3条路线:项目/一个,项目/两个和项目/三个,它们都指向项目虚拟机/视图。

I have 3 routes: items/one, items/two, and items/three and they're all pointing to 'items' vm/view.

items.js激活功能,我正在检查url,并在此基础上更改过滤器:

in the items.js activate function, I'm checking the url, and based on that, I'm changing a filter:

function activate(r) {
        switch (r.routeInfo.url) {
            case 'items/one': vm.filterType(1); break;
            case 'items/two': vm.filterType(2); break;
            case 'items/three': vm.filterType(3); break;
        }
        return init(); //returns a promise
    }

项目视图具有一个菜单,其中有一个按钮,二,三。
每个按钮都链接到如下操作:

The items view has a menu with buttons for one, two, and three. Each button is linked to an action like this:

function clickOne() {
    router.navigateTo('#/items/one');
}
function clickTwo() {
    router.navigateTo('#/items/two');
}    
function clickThree() {
    router.navigateTo('#/items/three');
}

这一切都可行,并且在视图上得到了正确的过滤器。但是,我注意到,如果我处于 one状态,然后转到 two状态,则ko绑定变量将以实时更新,也就是说,随着它们的变化以及激活之前许诺解决,这导致过渡发生两次(因为在获取数据时,并且在激活函数返回之后)。

this all works and I get the right filter on the view. However, I've noticed that if I'm on 'one', and then go to 'two', the ko-bound variables update in 'real-time', that is, as they're changing, and before the activate promise resolves, which causes the transition to happen twice (as the data is being grabbed, and after the activate function returns).

仅在这种情况下, view和viewmodel与上一个相同。我知道这是一种特殊情况,路由器可能正在使用areSameItem = true处理新路由的加载。我可以将VM / View分为三部分,并尝试从基本模型继承,但是我希望有一个更简单的解决方案。

This only happens in this scenario, where the view and viewmodel are the same as the previous one. I'm aware that this is a special case, and the router is probably handling the loading of the new route with areSameItem = true. I could split the VMs/Views into three and try to inherit from a base model, but I was hoping for a simpler solution.

推荐答案

我能够通过在包含div的项目上使用 ko.cleanNode()删除导航之前简单地删除ko绑定来解决此问题。

I was able to solve the issue by simply removing the ko bindings before navigation using ko.cleanNode() on the items containing div.

这篇关于Durandal:多条路线,一个视图模型/视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆