Created
June 18, 2015 10:56
-
-
Save gladiatorAsh/4a00d208f6cb9924cae2 to your computer and use it in GitHub Desktop.
Knockout datepicker
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
/** | |
* Function to bind Datepicker to the element passed | |
*/ | |
ko.bindingHandlers.datepicker = { | |
after: ['attr'], | |
init: function (element, valueAccessor, allBindingsAccessor) { | |
//initialize datepicker with some optional options | |
var options = allBindingsAccessor().datepickerOptions || { | |
dateFormat: GlbDatepickerOptions.dateFormat, timeFormat: GlbDatepickerOptions.timeFormat, dateonly: GlbDatepickerOptions.dateonly, changeMonth: GlbDatepickerOptions.changeMonth, | |
changeYear: GlbDatepickerOptions.changeYear | |
}; | |
$(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); | |
//daterestriction(element); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment