I wanted to avoid lumping onto this existing question.
So I'm trying to use that JSFiddle but within the Durandal framework where I have the main.js configured with the require bindings (please notice the observable: true):
define(['durandal/system', 'durandal/app', 'durandal/viewLocator', 'modules/logger', 'modules/bindings', 'modules/hubService'], function (system, app, viewLocator, logger, bindings, hubService) {
app.configurePlugins({
router: true,
dialog: true,
widget: true,
observable: true
});
I've set up a bindings.js with the composite binding handler like so:
composition.addBindingHandler('datePicker', {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var unwrap = ko.utils.unwrapObservable;
var dataSource = valueAccessor();
var binding = allBindingsAccessor();
var options = {
keyboardNavigation: true,
todayHighlight: true,
autoclose: true,
format: 'mm/dd/yyyy'
};
if (binding.datePickerOptions) {
options = $.extend(options, binding.datePickerOptions);
}
$(element).datepicker(options);
$(element).datepicker('update', dataSource);
$(element).on("changeDate", function (ev) {
var observable = valueAccessor();
if ($(element).is(':focus')) {
// Don't update while the user is in the field...
// Instead, handle focus loss
$(element).one('blur', function (ev) {
var dateVal = $(element).datepicker("getDate");
observable(dateVal);
});
}
else {
observable(ev.date);
}
});
//handle removing an element from the dom
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
$(element).datepicker('remove');
});
},
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
$(element).datepicker('update', value);
}
});
While this works in the JSFiddle, it throws an error when using the code in the Durandal framework.
The View comes up and I can use the datepicker but it throws an error on the following line:
observable(dateVal);
Anyone have a clue as to how to get this to work with Durandal's usage of observables?