Skip to content
Advertisement

Handlebars re-compile templates

I’m working with handlebars and I have this situation.

I do a request to a server, and get all the data I need to put it on the views. I use handlebars to put that data, and have no problem.

Now, I need to do the request every 1 or 2 minutes. But when I’m going to put the data again in the views, there’s an error. This is because the template I’ve already compiled and the tag where the template will be inserted isn’t there any more.

Any solutions? I left my code here:

In my view I have this:

JavaScript

And in my JavaScript, I have this:

JavaScript

The error is this:

Uncaught Error: You must pass a string or Handlebars AST to Handlebars.compile. You passed undefined

I know the error is that when I pass $('#vessels-info').html(); to the compile function, the tag <script> with the ID ‘vessels-info’ is not there any more, cause was already compiled.

Any help?

Advertisement

Answer

In this case you can keep a reference to the template as a string. Something like:

JavaScript

Putting a variable like this in the global scope is bad practice. And if your application grows more complicated I would suggest creating an object which would then hold the state and do the periodic updates.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement