English 中文(简体)
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"?

最佳回答

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.

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签