1 视图模型多条路线:生命周期 [英] 1 viewmodel multiple routes: lifecycle

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

问题描述

我希望有几个不同的路由指向同一个视图/视图模型,我已经设法实现了这一点.

I'd like to have a couple of different routes pointing to the same view/viewmodel and I have managed to make this happen.

            { route: 'formulation',             moduleId: 'formulation',    title: 'Formulation',       nav: 6 },
            { route: 'fabrication',             moduleId: 'test',           title: 'Fabrication',       nav: 7 },
            { route: 'fabrication/:studyId',    moduleId: 'test',           title: 'Fabrication'               },
            { route: 'characterization',        moduleId: 'test',           title: 'Characterization',  nav: 8 },

但是,我在生命周期方面遇到了一些问题.当散列从一个散列更改为另一个散列时,我希望收到通知.例如制造到表征.理想情况下,我可以只更新一些变量,但我不反对重建视图/视图模型.问题是你们中的一些人可能知道什么时候将 moduleId 更改为与之前的 moduleId 相同,但不会触发 activate 钩子.这究竟应该如何处理.谢谢,卡尔文

However, I'm running into a bit of a problem with the lifecycle. I'd like to be notified when the hash changes from one hash to another hash. For example fabrication to characterization. Ideally I could just update a few variables but I'm not against rebuilding the view/viewmodel. The issue is as some of you might know when is when the changing moduleId to is the same as the previous moduleId the activate hook does not fire. How exactly should this be handled. Thanks, Calvin

推荐答案

我认为最好的办法是覆盖激活器上的 areSameItem(由路由器创建为 activeItem代码>).在应用程序连接的早期,尝试这样做:

I think your best bet is to override areSameItem on the activator (which is created by the router as activeItem). Somewhere early on in the wireup of your application, try doing this:

var routerAreSameItem = router.activeItem.settings.areSameItem;

router.activeItem.settings.areSameItem = function (currentItem, newItem, currentActivationData, newActivationData) {
    debugger;
    return routerAreSameItem.apply(this, _.toArray(arguments));
};

显然这个特定的实现不会做任何事情;但是您应该能够在 areSameItem 方法中提供一些自定义逻辑来区分项目更改.

Obviously this particular implementation isn't going to do anything; but you should be able to provide some custom logic in the areSameItem method to distingish the item change.

如果我没记错的话,从那个方法返回 false 应该会让你的 activate 方法被调用.

If my memory serves correctly, returning false from that method should make your activate method get called.

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

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