Created
May 21, 2013 17:45
-
-
Save andrewburgess/5621735 to your computer and use it in GitHub Desktop.
KnockoutJS binding with Twitter Bootstrap's modal dialog
This file contains 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
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