简体   繁体   English

是否可以每次在Meteor模板中使用唯一变量?

[英]Can I use a unique variable in Meteor template each time?

In another template, I'm using 'articleSingle' template inside a loop. 在另一个模板中,我在循环内使用“ articleSingle”模板。

{{#each articles}}
    {{> articleSingle}}
{{/each}}

The articleSingle template has this: articleSingle模板具有以下功能:

let publishTitleNonReactive;

Template.articleSingle.onCreated(function() {

    this.autorun(function() {
        if (Template.currentData() && !publishTitleNonReactive) {
            publishTitleNonReactive = Template.currentData().publishTitle;
        }
    });

});

I want this publishTitleNonReactive var to be unique or at least in a local scope so that I can have different value each time this template is being used. 我希望此publishTitleNonReactive是唯一的,或者至少在本地范围内,以便每次使用此模板时我可以具有不同的值。

Any idea how can I get there? 知道如何到达那里吗?

Update: 更新:

I'm trying to use the variable in a helper which is reactive right now. 我正在尝试在现在是反应性的助手中使用变量。 So, whenever the template data updates, it becomes undefined. 因此,每当模板数据更新时,它就变得不确定。

The helper I'm using in 'articleSingle' template: 我在“ articleSingle”模板中使用的帮助器:

titleHybrid: function(){
    return this.publishTitle;
}

You can store properties in the template instance by using this in onCreated : 您可以在onCreated使用this属性将属性存储在模板实例中:

Template.articleSingle.onCreated(function () {
  this.publishTitleNonReactive = …
  …
});

i think i understand what you're asking; 我想我明白你的要求; can't you just push down the article data in each iteration? 您不能在每次迭代中仅下推文章数据吗?

{{#each article in articles}}
    {{> articleSingle article=article}}
{{/each}}

Template.articleSingle.onCreated(function() {
    let publishTitle = Template.currentData().article.publishTitle;
    this.publishTitle = new ReactiveVar(publishTitle);
});

or: 要么:

{{#each article in articles}}
    {{> articleSingle publishTitle=article.publishTitle}}
{{/each}}

Template.articleSingle.onCreated(function() {
    let publishTitle = Template.currentData().publishTitle;
    this.publishTitle = new ReactiveVar(publishTitle);
});

from there you can store the title in a reactive var to use it in helpers. 从那里,您可以将标题存储在反应式var中,以在助手中使用它。 you're keeping it in such a way that all instances of the template are sharing it. 您将其保留为模板的所有实例都共享它的方式。

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

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