Skip to content

Instantly share code, notes, and snippets.

@ssajous
Last active April 12, 2018 13:39
Show Gist options
  • Save ssajous/6057088 to your computer and use it in GitHub Desktop.
Save ssajous/6057088 to your computer and use it in GitHub Desktop.
Knockout.js binding handle to hide and show bootstrap modal dialog. jsFiddle example http://jsfiddle.net/shub2079/TAtvU/
// Custom binding for modal dialog
// Bind a Bootstrap modal div to an observable
// boolean. When the observable changes, the
// modal window's visibility will also change.
//
// The binding is two-way, so changes to the
// modal window's visibility will also change
// the bound observable's value
ko.bindingHandlers.bootstrapShowModal = {
init: function (element, valueAccessor) {
$(element).on('hidden', function () {
var value = valueAccessor();
value(false);
});
$(element).on('shown'), function () {
var value = valueAccessor();
value(true);
}
},
update: function (element, valueAccessor) {
var value = valueAccessor();
if (ko.utils.unwrapObservable(value)) {
$(element).modal('show');
// this is to focus input field inside dialog
$("input", element).focus();
}
else {
$(element).modal('hide');
}
}
};
@anthonyw12123
Copy link

For what it's worth, I read your placeholder text. It's like Lorem Ipsum for meat. Ron Swanson would be proud.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment