1 viewmodel multiple routes: lifecycle

て烟熏妆下的殇ゞ 提交于 2020-01-11 10:15:13

问题


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 },

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


回答1:


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));
};

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.

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



来源:https://stackoverflow.com/questions/22077884/1-viewmodel-multiple-routes-lifecycle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!