laconicdev
laconicdev

Reputation: 6440

Datepicker inside a handlebars template

I am trying to figure out a way to have jqueryui datepicker inside handlebars template. So far I have the following that doesn't seem to work:

>     <script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script>
>      <script>
>          $(function () {
>              $(".datePicker").datepicker();
>          });   </script>




 <script id="template-row" type="text/x-handlebars-template">
        <div class="returnRow{{#if altRow}} returnRow-alt{{/if}} clearfix">
            <input type="hidden" class="return-id" value="{{ID}}" />
            <input type='hidden' class='return-last-updated' value='{{LastUpdated}}' />
            <div class="pull-left returnRow-date">
                <input type="text" class="datePicker" />
             </div>
            <div class="pull-left returnRow-return">
                <input type="text" style="width: 90%" class="return-value" onchange='SaveSingleRow(this)' value="{{textValue}}" />
            </div>
            <div class="pull-left returnRow-message">
                <span class="row-message"></span>
            </div>
        </div>
    </script>

Upvotes: 0

Views: 3147

Answers (1)

mu is too short
mu is too short

Reputation: 434765

I think you have two options:

  1. Bind the datepicker after adding the filled-in template to the DOM.
  2. Bind the datepicker to a jQuery-wrapped version of the filled-in template and then add that jQuery object to the DOM.

The first option looks like this:

var t = Handlebars.compile($('#template-row').html());
$(something_already_in_the_dom).append(t(data));
$('.datePicker').datepicker();

Demo: http://jsfiddle.net/ambiguous/w2wyU/7/

The second option looks like this:

var t = Handlebars.compile($('#template-row').html());
var $html = $(t(data));
$html.find('.datePicker').datepicker();
$(something_already_in_the_dom).append($html);

Demo: http://jsfiddle.net/ambiguous/PMP8R/

Note that both options take place outside the template. The underlying problem is that .datepicker needs to be called on a jQuery object but everything is only text inside the template, you can't have a jQuery wrapped version of that text until after the template has been processed and filled in.

Upvotes: 4

Related Questions