Skip to content

Instantly share code, notes, and snippets.

@andrewburgess
Created May 21, 2013 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewburgess/5621735 to your computer and use it in GitHub Desktop.
Save andrewburgess/5621735 to your computer and use it in GitHub Desktop.
KnockoutJS binding with Twitter Bootstrap's modal dialog
ko.bindingHandlers.modal = {
init: function (element, valueAccessor, bindingsAccessor, viewModel, bindingContext) {
var $element = $(element);
$element.addClass('hide modal');
$element.modal({
show: false
});
var bindings = bindingsAccessor();
if (bindings.modalOptions) {
if (bindings.modalOptions.beforeClose) {
$element.on('hide', function () {
return bindings.modalOptions.beforeClose();
});
}
}
return ko.bindingHandlers['with'].init.apply(this, arguments);
},
update: function (element, valueAccessor, bindingsAccessor, viewModel, bindingContext) {
var value = ko.utils.unwrapObservable(valueAccessor());
var returnValue = ko.bindingHandlers['with'].update.apply(this, arguments);
var $element = $(element);
if (value) {
$element.modal('show');
setTimeout(function () {
$element.find('input').filter(':first').focus();
}, 200);
}
else
$element.modal('hide');
return returnValue;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment