Search code examples
jqueryjquery-uijquery-ui-datepickerrivets.js

RivetsJS: Update model with jQuery-ui Datepicker


I'm new in and I love it! Very useful in micro sites or small code fragments when other complete frameworks such AngularJS are too heavy.

I can make it work in all cases except when the input values changes via jQuery Plugin, a Datepicker in this particular case.

rivets.configure({ prefix: "data-rv", templateDelimiters: ['{{', '}}'] });
var $data = {CDP:null};

rivets.bind($("#toBind"), { data: $data }); 

function Datepicker(selector, context) {
            //
            $(selector, context).datepicker().keyup(function (e) {
                if (e.keyCode == 8 || e.keyCode == 46) {
                    $.datepicker._clearDate(this);
                }
            })
        }
        
$data.CDP = {LastDeliveryDate: "01/01/2015"};

Datepicker(".date");
<link href="https://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rivets/0.8.1/rivets.bundled.min.js"></script>
<div id="toBind">
  <input type="text" class="form-control date" id="txtDeliveryDate" placeholder="dd/mm/yyyy" data-rv-value="data.CDP.LastDeliveryDate">
  <span id="showText">{{data.CDP.LastDeliveryDate}}</span>
</div>

Here is the fiddle: https://jsfiddle.net/sLdvegdo/2/

As you see, value changes using the Datepicker selection dont update the model, but manual input does.

Any thoughts?


Solution

  • The reason it doesn't work is because the rv-value binder works by listening to input event (change in older version)

    You can fix this by manually triggering the event onSelect as shown below:

    rivets.configure({
      prefix: "data-rv",
      templateDelimiters: ['{{', '}}']
    });
    var $data = {
      CDP: null
    };
    
    rivets.bind($("#toBind"), {
      data: $data
    });
    
    function Datepicker(selector, context) {
      //
      $(selector, context).datepicker({
        onSelect: function(date) {
          $(this).trigger('input');
        }
      }).keyup(function(e) {
        if (e.keyCode == 8 || e.keyCode == 46) {
          $.datepicker._clearDate(this);
        }
      })
    }
    
    $data.CDP = {
      LastDeliveryDate: "01/01/2015"
    };
    
    Datepicker(".date");
    <link href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/rivets/0.8.1/rivets.bundled.min.js"></script>
    <div id="toBind">
      <input type="text" class="form-control date" id="txtDeliveryDate" placeholder="dd/mm/yyyy" data-rv-value="data.CDP.LastDeliveryDate">
      <span id="showText">{{data.CDP.LastDeliveryDate}}</span>
    </div>