I am trying to fully bind twitter bootstrap modal with knockout. By fully bind I mean that I want every close interaction with modal dialog to work with knockout. I have seen some of the questions, which partially bind them (for example this one does not allow esc).
I am using almost identical binding (which I actually found elsewhere)
ko.bindingHandlers.modal = {
init: function (element, valueAccessor) {
$(element).modal({
show: false
});
},
update: function (element, valueAccessor) {
var value = valueAccessor();
if (ko.utils.unwrapObservable(value)) {
$(element).modal('show');
} else {
$(element).modal('hide');
}
}
}
But the problem is that not everything work in my Fiddle. As you see if you close Modal with Close button, you can fire this modal again. But if you close it with Esc key, or with clicking on background, or on the X button, you can not open Modal again.
I know that the problem is due to the fact that when I close modal with other means (it is not changing observable and therefore when I fire it for the second time - nothing changes). But I can not figure out how to do this properly.
Here is my hack :-), where everything works. I am giving new value each time. But is there a better way?
slightly neater BS binding code - and classes are added when needed.:
Then just
data-bind="BSModal: true/false Observable"
value.bootstrap modal provided events, you just need to hook up event 'hidden.bs.modal'.
BTW, do proper disposal too. http://jsfiddle.net/C8w8v/377/