Skip to content

Instantly share code, notes, and snippets.

@keepitsimple
Created February 18, 2015 10:23
Show Gist options
  • Save keepitsimple/604ef316a20242f1c3c9 to your computer and use it in GitHub Desktop.
Save keepitsimple/604ef316a20242f1c3c9 to your computer and use it in GitHub Desktop.
jQuery UI Datepicker bindings for Knockout.js
########################################
## jQuery UI Datepicker bindings
#
ko.bindingHandlers.datepicker =
init: (element, valueAccessor, allBindingsAccessor) ->
options = allBindingsAccessor().datepickerOptions || {}
$(element).datepicker options
#/handle the field changing
ko.utils.registerEventHandler element, "change", () ->
observable = valueAccessor();
observable $(element).datepicker("getDate")
#handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback element, () ->
$(element).datepicker "destroy"
#update the control when the view model changes
update: (element, valueAccessor) ->
value = ko.unwrap valueAccessor()
current = $(element).datepicker("getDate")
if (value - current) != 0
$(element).datepicker("setDate", value)
# html example
# <input id="d_from" data-bind="datepicker: dFrom, datepickerOptions: {language: 'ru', minDate: new Date(), dateFormat: 'dd.mm.yy'}" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment