简体   繁体   English

dust.js模板数据更新

[英]dust.js template data update

is there a way to update the template data without re-rendering the dust template. 有没有一种方法可以更新模板数据而无需重新渲染灰尘模板。

What i mean is this. 我的意思是这个。

I have a successful dust template that outputs the following: 我有一个成功的灰尘模板,可以输出以下内容:

<ul>
    <li>John</li>
    <li>Mark</li>
    <li>Jim</li>
    <li>Nick</li>
</ul>

when i do an update on my data, for example: change "John" to "Peter" and sent the result to the database, do I need to re-render the dust template again (which will take all data and re-draw it on the client) or is there any way to tell dust to update ONLY the li with the value "John" ( and change it to "peter")? 当我对数据进行更新时,例如:将“ John”更改为“ Peter”并将结果发送到数据库,我是否需要再次重新渲染尘土模板(它将获取所有数据并重新绘制它)或者是否有任何方法告诉灰尘仅将li的值更新为“ John”(并将其更改为“ peter”)?

You could just use smaller template components. 您可以只使用较小的模板组件。 Here's what I mean: 这就是我的意思:

list.tl: list.tl:

<ul>
  <li>{>"listItem" name="John"/}</li>
  <li>{>"listItem" name="Mark"/}</li>
  <li>{>"listItem" name="Jim"/}</li>
  <li>{>"listItem" name="Nick"/}</li>
</ul>

listItem.tl: listItem.tl:

{name}

Then when you, let's say, click on an element, you could do something like this: 然后,假设您单击某个元素,则可以执行以下操作:

document.getElementsByTagName('li').addEventListener('click', function() {
  var element = this;
  dust.render('listItem', {name: 'MyNewName'}, function(error, output) {
    element.innerHTML = output;
  });
}

With this, you only re-render the part that is actually being changed. 这样,您只需重新渲染实际更改的零件。

EDIT 1: You don't need the .tl extension when calling another template, so got rid of that. 编辑1:调用另一个模板时,您不需要.tl扩展名,所以摆脱了它。

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

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