Durandal router / lifecycle events when using a childRouter

旧巷老猫 提交于 2019-12-21 16:55:31

问题


In a single page app built using Durandal, I have a settings view, with different sections. I use a childRouter to resolve the different sections.

In this application, a lot of code hooks into the composition lifecycle events that Durandal uses. For example, this custom binding:

ko.bindingHandlers.autoFocus = {
    init: function (element) {
        var $element = $(element),
            setFocus = function () {
                $element.focus();
            };

        router.on('router:navigation:composition-complete', setFocus);
        ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
            router.off('router:navigation:composition-complete', setFocus);
            $element = null;
        });
    }
};

This custom binding has functioned perfectly so far, setting focus to the field the binding was applied to when the view is displayed.

However, with the child router, the timing is off. It seems that the main router triggers the 'router:navigation:composition-complete' event when the 'settings' view is loaded. At this point, the child view is not yet displayed, and the element with the autofocus binding does not get the focus. This is illustrated even more clearly when navigating between settings pages: on leaving a page, the field with the autofocus binding on the old child view gets the focus, then the new child view is displayed without a focused element.

Is there any way to get the child router to omit events as well? Or possibly to get the main router to postpone the events till the child router is done?

I've also read that this applies in other stages of composition as well: e.g. the deactivate callback will not be called on the viewmodel of a child view. So a general fix for event triggering on child routers would be the ideal solution I'm looking for.

Thanks in advance.


回答1:


Try this:

ko.bindingHandlers.autoFocus = {
    init: function (element) {
        var $element = $(element),
            setFocus = function (child, parent, context) {
                $element.focus();
                        if (child.router)
                            child.router.on('router:navigation:composition-complete', function () {    $element.focus(); });
};
        router.on('router:navigation:composition-complete', setFocus);
        ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
            router.off('router:navigation:composition-complete', setFocus);
            $element = null;
        });
    }
};


来源:https://stackoverflow.com/questions/21433680/durandal-router-lifecycle-events-when-using-a-childrouter

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