Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebar.js not refreshing my template

OK. My HTML looks like below.

<div id="json"></div>

<div id="content-placeholder">
  <script id="some-template" type="text/x-handlebars-template">
    <table>
      <thead>
        <th>col 1</th>
        <th>col 2</th>
      </thead>
      <tbody>
        {{#results}}
        <tr>
          <td>{{col_1}}</td>
          <td>{{col_2}}</td>
        </tr>
        {{/results}}
      </tbody>
    </table>
  </script>                     
</div>

And I am populating the above via Handlebar.js and the data is received from the server. Here's the code.

$.get(get_data_url, function(data)
{
  $('#json').empty().append(data);
  var rows = eval('(' + data + ')');

  var source   = $("#some-template").html();
  var template = Handlebars.compile(source);                                
  $("#content-placeholder").empty().append(template(rows));
});

When the code runs the first time, it looks fine. But when I call the $.get the second time (and so forth), the template is not refreshed with the new data.

I also print out the whole data string in , to make sure that data is being refreshed from the server and it is.

When I check on my Chrome, it tells me "Uncaught TypeError: Cannot call method 'match' of null".

Is it something to do with "compile"?

like image 930
ericbae Avatar asked Aug 19 '11 03:08

ericbae


1 Answers

The first time you do this:

$("#content-placeholder").empty()...

Your <div> turns into this:

<div id="content-placeholder">
</div>

And your template is gone. Move your template:

<script id="some-template" type="text/x-handlebars-template">
    ...
</script>

to somewhere outside #content-placeholder.

like image 59
mu is too short Avatar answered Sep 23 '22 08:09

mu is too short