jQuery UI datepicker binding

http://www.knockmeout.net/2011/07/another-look-at-custom-bindings-for.html

by kougiland

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.2.1.debug.js"></script>
<input data-bind="datepicker: myDate, datepickerOptions: { minDate: new Date() }" />

<hr />

<button data-bind="click: setToCurrentDate">Set To Current Date</button>

<hr />

<div data-bind="text: myDate"></div>

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 = {
    myDate: ko.observable(new Date("11/01/2011")),
    setToCurrentDate: function() {
       this.myDate(new Date());   
    }
};

ko.applyBindings(viewModel);