I need to attach a bootstrap datepicker to two input fields that also need a value binding since I need to be able to dynamically set the value of the input according to changes in my observable.
So far, the binding works only one way: When I pick a date in the datepicker, the observable is correctly updated. But when I change the value of the attached observable in my viewmodel, the input does not reflect the change.
This is my binding handler:
ko.bindingHandlers.bootstrapDP = {
init: function (element, valueAccessor, allBindingsAccessor) {
var options = allBindingsAccessor().datepickerOptions || {};
$(element).bootstrapDP(options).on("changeDate", function (ev) {
var observable = valueAccessor();
observable($(element).val());
});
},
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
$(element).bootstrapDP("setValue", value);
}
};
In my viewmodel I have an object encapsulating startDate and endDate:
self.dateFilter = {
startDate: ko.observable(),
endDate: ko.observable()
};
This is my HTML:
<input type="text" data-bind="bootstrapDP: dateFilter.startDate, value: dateFilter.startDate" />
<input type="text" data-bind="bootstrapDP: dateFilter.endDate, value: dateFilter.endDate" />
I'm using this datepicker library (in noConflict()
-mode): https://github.com/eternicode/bootstrap-datepicker
DOCS here: http://bootstrap-datepicker.readthedocs.org/en/stable/
What will I need to add/change/do differently to get the desired result?