I am following the example here to create a custom binding. The property to be updated is obtained like this in the documentation:
var value = valueAccessor();
value(false);
In my code, a table is bound to an array of objects using knockout's foreach binding.
<tbody data-bind="foreach: {data: sites, as: 'site'}">
<td class="siteid" data-bind="text: site.siteid"></td>
<td class="expirydate" data-bind="datepicker: site.expirydate"></td>
et cetera
The table seems to be correctly populated with data. The values are all in the right place.
The objects in the array have a few string properties and a couple of dates, so several of the table cells have jQuery UI datepickers attached to them. Here's the custom datepicker binding code I'm using, which I found here on SO, though I don't have the reference handy (it was from a response by Mr Niemeyer, as I recall).
ko.bindingHandlers.datepicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
//initialize datepicker with some optional options
var options = allBindingsAccessor().datepickerOptions || {},
$el = $(element);
$el.datepicker(options);
//handle the field changing
ko.utils.registerEventHandler(element, "change", function () {
var observable = valueAccessor();
var newVal = $el.datepicker("getDate")
observable(newVal);
// a breakpoint is set at this location
});
.
.
. <snip>
The code crashes on observable(newVal);
However, if I change
var observable = valueAccessor();
to
var observable = valueAccessor;
then the code runs without error. But when I examine the value in observable afterwards, at the breakpoint location, it does not contain the date value in newVal; rather it contains the original date value.
And if instead of observable(newVal) I try this instead:
allBindingsAccessor().value(newVal);
I get an error: " Object doesn't support property or method 'value'".
ko.unwrapand later pass it .later pass it. I want to update the model with the current date value from the datepicker. The user has made an edit to the row. Not sure how to get the date into the model, when using the custom binding.