jQuery UI datepicker binding
https://groups.google.com/d/topic/knockoutjs/_QbqkEffEW4/discussion
by Rahul Patil
HTML
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/base/jquery-ui.css">
<script src="http://knockoutjs.com/downloads/knockout-2.0.0.debug.js"></script>
<ul>
<li>
<input data-bind="datepicker: newDate" />
</li>
</ul>
<hr/>
<div data-bind="text: ko.toJSON($root)"></div>
CSS
li { margin: 5px; }
JavaScript
ko.bindingHandlers.datepicker = {
init: function(element, valueAccessor, allBindingsAccessor) {
//initialize datepicker with some optional options
var options = allBindingsAccessor().datepickerOptions || {};
$(element).datepicker(options);
//handle the field changing
ko.utils.registerEventHandler(element, "change", function () {
var observable = valueAccessor();
observable($(element).datepicker("getDate"));
});
//handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$(element).datepicker("destroy");
});
},
//update the control when the view model changes
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor()),
current = $(element).datepicker("getDate");
if (value - current !== 0) {
$(element).datepicker("setDate", value);
}
}
};
var viewModel = {
newDate : ko.observable('2014-03-13T18:30:00.000Z')
};
ko.applyBindings(viewModel);