【发布时间】:2019-02-14 21:38:32
【问题描述】:
我正在使用 mustache js 使用来自 API 的数据渲染模板并且效果很好,但我需要在一段时间后更新(重新渲染)相同的模板。就我而言,我在模板中有一个列表,如下所示:
template.html
<div id="template">
{{#list}}
<span>{{firstName}} {{lastName}} - {{phone}}</span>
{{/list}}
</div>
index.js
$(document).ready(function(){
$.ajax(
//some ajax here
).done(function(response){
loadTemplate(response);
});
});
function loadTemplate(data){
var template = $("#template").html();
Mustache.parse(template);
var render = Mustache.to_html(template, data);
$("#template").empty().html(render);
};
但是用户可以在这个列表中添加更多元素,之后我需要更新 mustache 模板。我尝试调用 Ajax(在列表中添加新值的响应)然后再次调用 loadTemplate 函数但不起作用,列表不会用新值更改(更新)。
【问题讨论】:
-
不工作太笼统了。似乎您每次都覆盖模板。请说明 - 您的响应的结构是什么,它是代表单个项目(需要添加到模板中)还是项目列表(应该覆盖整个模板)
-
@ymz,谢谢...我编辑了问题,你能帮忙吗?
标签: javascript mustache