knockoutjs databind with jquery controls
http://stackoverflow.com/questions/6399078/knockoutjs-databind-with-jquery-controls
HTML
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/themes/base/jquery-ui.css">
<script src="http://knockoutjs.com/downloads/knockout-2.2.1.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.4.0/moment.js"></script>
<input data-bind="datepicker: myDate" />
<input data-bind="timepicker: myDate" type="time" />
<hr />
<button data-bind="click: setToCurrentDate">Set To Current Date</button>
<button data-bind="click: showCurrentDateTime">Show current date</button>
<hr />
<div data-bind="text: myDate"></div>
CSS
td, th {
padding: 5px;
border: solid 1px black;
}
th {
color: #666;
}
a {
font-size: .75em;
}
JavaScript
ko.bindingHandlers.datepicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
var $el = $(element);
//initialize datepicker with some optional options
var options = allBindingsAccessor().datepickerOptions || {};
$el.datepicker(options);
//handle the field changing
ko.utils.registerEventHandler(element, "change", function () {
var observable = valueAccessor();
var oldDate = observable();
var newDate = $el.datepicker("getDate");
var newDate = new Date(newDate.toLocaleDateString() + " " + oldDate.toLocaleTimeString());
observable(newDate);
});
//handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
$el.datepicker("destroy");
});
},
update: function (element, valueAccessor) {
var d = viewModel.myDate();
if (d!=null) alert(d);
var value = ko.utils.unwrapObservable(valueAccessor()),
$el = $(element),
current = $el.datepicker("getDate");
if (value - current !== 0) {
$el.datepicker("setDate", value);
}
}
};
ko.bindingHandlers.timepicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
// Get the value of the value binding
var value = valueAccessor();
var thisElement = $(element);
console.log('The value of the element should be here - ', value());
// If the value is not a valid moment in time,
console.log(!moment(value()).isValid());
//handle the field changing
ko.utils.registerEventHandler(element, "change", function () {
var observable = valueAccessor();
console.log('observable - ', observable());
var tzdstOffset = (observable().getTimezoneOffset() + (moment(observable()).isDST() ? 60 :...