1 viewmodel的多条路线:生命周期 [英] 1 viewmodel multiple routes: lifecycle
问题描述
我希望有两条指向相同视图/视图模型的不同路线,并且我设法做到了这一点.
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相同时,激活挂钩不会触发.应该如何处理. 谢谢, 加尔文(Calvin)
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 viewmodel的多条路线:生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!