最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Handlebar.js not refreshing my template - Stack Overflow

programmeradmin2浏览0评论

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 = Handlebarspile(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 "pile"?

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.pile(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 "pile"?

Share Improve this question edited Aug 19, 2011 at 3:10 mu is too short 435k71 gold badges858 silver badges818 bronze badges asked Aug 19, 2011 at 3:04 ericbaeericbae 9,64426 gold badges77 silver badges109 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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.

发布评论

评论列表(0)

  1. 暂无评论