Twitter-Bootstrap Modal Bindinghandler with Knockout breaking if backdrop is clicked

醉酒当歌 提交于 2019-12-24 08:22:25

问题


I'm trying to disable the backdrop from closing the modal when clicked with the following:

ko.bindingHandlers.showModal = {
    init: function (element, valueAccessor) {
    },
    update: function (element, valueAccessor) {
        var value = valueAccessor();
        if (ko.utils.unwrapObservable(value)) {
            $(element).modal({ backdrop: 'static', keyboard: true });
            // this is to focus input field inside dialog
            $("input", element).focus();
        }
        else {

            $(element).modal('hide');
        }
    }
};

From what I understand, $(element).modal({ backdrop: 'static', keyboard: true }); should achieve what I'm after. But if I click on the backdrop the modal still closes though, and in fact breaks the "show" button. What am I doing wrong here?

Fiddle: http://jsfiddle.net/PTSkR/175/


回答1:


You problem is that you are trying to initialize your modal multiple times.

The initialization logic where you set backdrop: 'static' should go into the init function (which is called only once) and you need to call .modal('show') and .modal('hide') in your update function:

ko.bindingHandlers.showModal = {
    init: function (element, valueAccessor) {
        $(element).modal({ backdrop: 'static', keyboard: true, show: false });
    },
    update: function (element, valueAccessor) {
        var value = valueAccessor();
        if (ko.utils.unwrapObservable(value)) {
            $(element).modal('show');
            $("input", element).focus();
        }
        else { 
            $(element).modal('hide');
        }
    }
};

Demo JSFiddle.



来源:https://stackoverflow.com/questions/18370489/twitter-bootstrap-modal-bindinghandler-with-knockout-breaking-if-backdrop-is-cli

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