【发布时间】:2014-07-16 16:57:44
【问题描述】:
我有在按钮点击时打开的引导模式窗口。一切正常,除了一件事:当我单击非模态空间时 - 模态窗口关闭,但我可以在单击按钮时再次打开它。如果我在按钮关闭时关闭模式 - 它运作良好。
更新:当我使用“Esc”键盘关闭模式时 - 行为与非模式单击相同。
HTML
<button data-bind="click: function () { $root.showLogModal(); }" title="Show Logs" class="btn btn-large btn-info">Show Logs</button>
<div data-bind="bootstrapLogModal: logModal" tabindex="-1" role="dialog"></div>
自定义模板
<script id="showLogModal" type="text/html">
<div class="modal-header">
<button type="button" class="close" data-bind="click: close" aria-hidden="true">×</button>
<h3>Logs</h3>
</div>
<div class="modal-body">
<div class="alert alert-info">
Some Content
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn" data-bind="click: close">Close</button>
</div>
JS
function ViewModel() {
var self = this;
self.logModal = {
show: ko.observable(false),
onClose: function () {
},
onAction: function () {
}
};
self.showLogModal = function () {
self.logModal.show(true);
};
}
$(function () {
var viewModel = new ViewModel();
ko.bindingHandlers.bootstrapLogModal = {
init: function (element, valueAccessor, allBindingsAccessor, data, bindingContext) {
var props = valueAccessor(),
vm = bindingContext.createChildContext(data);
ko.utils.extend(vm, props);
vm.close = function () {
vm.show(false);
vm.onClose();
};
vm.action = function () {
vm.onAction();
};
ko.utils.toggleDomNodeCssClass(element, "modal hide fade large", true);
ko.renderTemplate("showLogModal", vm, null, element);
var showHide = ko.computed(function () {
$(element).modal(vm.show() ? 'show' : 'hide');
});
return {
controlsDescendantBindings: true
};
}
};
ko.applyBindings(viewModel);
});
http://jsfiddle.net/tLT6d/ 这里是 Live 示例。
【问题讨论】:
标签: javascript html knockout.js bootstrap-modal