Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While @amakhrov answer will work (and would be even better if used writeable computed observable like sujested by @Stijn) I decided to do this using <a href="http://knockoutjs.com/documentation/custom-bindings.html">Custom Bindings</a>.</p> <p>The main advantage of doing this is reusability - I just have to use <code>data-bind="datePicker : MyDate"</code> whereever I want to tie this in. I can also modify other properties of the input element so this could be really useful if binding to complex jQuery (and other) controls.</p> <p><em>(<a href="http://www.knockmeout.net/2011/03/reacting-to-changes-in-knockoutjs.html">Read here for more pro/cons</a> about the 3 choices to do this sort of thing)</em></p> <p>HTML</p> <pre><code>&lt;input data-bind="datePicker : MyDate" type="date"&gt; </code></pre> <p>JS</p> <pre><code>ko.bindingHandlers.datePicker = { init: function (element, valueAccessor, allBindingsAccessor, viewModel) { // Register change callbacks to update the model // if the control changes. ko.utils.registerEventHandler(element, "change", function () { var value = valueAccessor(); value(new Date(element.value)); }); }, // Update the control whenever the view model changes update: function (element, valueAccessor, allBindingsAccessor, viewModel) { var value = valueAccessor(); element.value = value().toISOString(); } }; var viewModel = { MyDate : ko.observable(new Date()) }; ko.applyBindings(viewModel); </code></pre> <p>See <a href="http://jsfiddle.net/LLkC4/5/">http://jsfiddle.net/LLkC4/5/</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload