1

A knockout datepicker data binder can be found here: jQuery UI datepicker change event not caught by KnockoutJS

In order to hide the dates (which is the only thing that triggers datepicker's output), I do this for the sans knockout solution:

$(".monthPicker").focus(function () {
    $(".ui-datepicker-calendar").hide();
});
$(".monthPicker").datepicker({
    dateFormat: 'MM yy,
    changeMonth: true,
    changeYear: true,
    showButtonPanel: true,
    onClose: function(dateText, inst) {
        var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
        var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
        $(this).val($.datepicker.formatDate('MM yy', new Date(year, month, 1)));
    }
});

This works.

But how do I get this working with knockout's data binder?

Community
  • 1
  • 1
Daniel Harvey
  • 381
  • 5
  • 16
  • 1
    check for double quotes inside onClose and change them to single quotes. You probably get errors using `data-bind="..... "something" ....."` – BartekR Jan 24 '12 at 14:09
  • That's parsing but not binding to selectedMonthDate, still. – Daniel Harvey Jan 24 '12 at 14:18
  • Posted the exact code I'm using and will keep it up to date. I'm trying to figure this and subsequently ko binders out. If I beat someone else to it, I'll post my answer. – Daniel Harvey Jan 24 '12 at 15:51

1 Answers1

1

The solution was to update the ViewModel directly and let the changes propogate to the datepicker via the update on the binder.

    <input data-bind="datepicker: monthDate,
          datepickerOptions:
          {
            changeMonth: true,
            changeYear: true,
            showButtonPanel: true,
            dateFormat: 'MM yy',
            onClose: function(dateText, inst) {
                var month = $('#ui-datepicker-div .ui-datepicker-month :selected').val();
                var year = $('#ui-datepicker-div .ui-datepicker-year :selected').val();
                $root.monthDate(new Date(year, month, 1));
            }
          }" />  
Daniel Harvey
  • 381
  • 5
  • 16
  • Can you please show your viewmodel changes? I am pretty new to Knockout and I am modifying someone else's code and I have no idea what to change. – liamfriel Mar 10 '14 at 10:27