Question

Has anyone successfully used jQuery Mobile DateBox with Knockout.js?

I'm having trouble getting the binding to work when providing a value to display before selecting a date. It remains blank. If I use a standard text input, the binding works fine.

<input id="start" type="date" data-bind="value: start" data-role="datebox" data-options='{"mode": "timeflipbox", "useButton": false, "useFocus": true, "dialogForce" : true, "transition" : "slidedown"}'>

//In the view model:
this.start = ko.observable(startTime);

I've tried

$('#start').datebox('refresh')

but no luck....

Was it helpful?

Solution

I ended up writing a custom binding for knockout which seems to do the trick:

    ko.bindingHandlers.jqmDateBox = {
    update: function (element, valueAccessor, allBindingsAccessor, context) {
        setTimeout(function () { 
            var value = valueAccessor();
            var valueUnwrapped = ko.utils.unwrapObservable(value);
            $(element).val(valueUnwrapped);
            $(element).data('datebox').options.defaultPickerValue = valueUnwrapped;
        }, 0);
    }
}

Usage:

<input id="end" name="end" type="date" data-bind="value: end, jqmDateBox: end" data-role="datebox" />

OTHER TIPS

The binding offered by David didn't work for me. I'm using the latest unstable version of Datebox.

I've written this binding, which works for me in calbox mode:

ko.bindingHandlers['dateboxvalue'] = {
  'init': function(element, valueAccessor, allBindingsAccessor, viewModel) {

     ko.utils.registerEventHandler(element, "change", function () {
       var value = valueAccessor();
       value($(element).datebox('getTheDate'));
  });
},
  'update': function (element, valueAccessor, allBindingsAccessor, context) {

    $(element).datebox();
    $(element).datebox('setTheDate', ko.utils.unwrapObservable(valueAccessor()));
    $(element).trigger('datebox', {'method':'doset'});
  }
};

use like this

<label for="mode1">CalBox</label>
<input name="mode1" id="mode1" type="text"
       data-role="datebox"
       data-options='{"mode":"calbox", "useNewStyle":true}'
       data-bind="dateboxvalue: myDate"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top