简体   繁体   English

带有更新数据的车把重新渲染模板

[英]Handlebars re-rendering template with updated data

{{#each dogs}}
<li>{{this.name}}</li>
{{/each}}

Imagine I use ajax to update my list above how do I recompile the template after it's done? 想象一下,我使用ajax更新了上面的列表,完成后如何重新编译模板? I can do location.reload() but that's not good for the UX. 我可以执行location.reload(),但这对UX不利。 Since I'm using handlebars I shouldn't use jquery to generate my html and update the DOM. 由于我使用的是车把,所以我不应该使用jquery来生成我的html和更新DOM。

You can recompile the template using the Handebars.compile(data) method as many times as you like. 您可以根据需要多次使用Handebars.compile(data)方法重新编译模板。 Who's says you can't use jQuery to generate and update the DOM when using Handlebars? 谁说使用把手时不能使用jQuery生成和更新DOM?

But, you don't need to recompile (thanx @dandavis!), in order to update the data. 但是,您无需重新编译(感谢@dandavis!)即可更新数据。 Here is a little demo, that compiles Handlebars template once, and then just updates the template data and appends it on the DOM using jQuery, after each new Ajax call: JSFiddle Demo 这是一个小示例,该示例一次编译Handlebars模板,然后在每次新的Ajax调用之后仅更新模板数据并将其使用jQuery附加到DOM上: JSFiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM