Meteor 防止模板辅助程序在不必要时重新运行的最佳方法?
我试图阻止模板帮助程序在不必要时重新运行。我制作了一个简单的应用程序来说明这种行为: 假设我想显示一些只包含标题和描述的项目Meteor 防止模板辅助程序在不必要时重新运行的最佳方法?,meteor,meteor-blaze,Meteor,Meteor Blaze,我试图阻止模板帮助程序在不必要时重新运行。我制作了一个简单的应用程序来说明这种行为: 假设我想显示一些只包含标题和描述的项目 <template name="Tests"> {{#each items}} {{> TestsItems}} {{/each}} </template> <template name="TestsItems"> <div class="title">{{title}}</div&
<template name="Tests">
{{#each items}}
{{> TestsItems}}
{{/each}}
</template>
<template name="TestsItems">
<div class="title">{{title}}</div>
<div class="description">{{description}}</div>
</template>
仅在标题字段上进行新更新时,可以看到重新运行描述帮助器。我试图实现的是,仅当description字段有新值时才重新运行此帮助程序,而不是每次文档中的字段发生更改时
由于{{{常量}}和{{{{孤立}}被弃用,我如何在最新的Meteor版本中获得这种行为
注意1:创建包含描述的新子模板并不能解决问题。我会避免模板帮助程序中的副作用。相反,我会使用自动运行:
Template.TestItems.rendered=函数(){
var _id=this.data._id;
这是一个自动运行(函数(){
//仅选择描述字段,以便我们仅
//如果描述字段更改,则触发重新运行
var description=Items.findOne(_id,{fields:{description:1});
//更新JQuery插件
});
}
太棒了!那很干净!
Template.Tests.helpers({
items: function () {
return Items.find();
}
});
Template.TestsItems.helpers({
description: function () {
// I'm using this helper to do some updates
// on a jQuery plugin when the description field change.
// see example 1: https://github.com/avital/meteor-ui-new-rendered-callback/
console.log("The description is run");
return this.description;
}
});