Last active
April 12, 2018 13:39
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For what it's worth, I read your placeholder text. It's like Lorem Ipsum for meat. Ron Swanson would be proud.