简体   繁体   English

在Meteor.js中将变量范围设置为模板

[英]Set a Variable Scope to Template in Meteor.js

Is it possible to create a variable that is scoped to a template? 是否可以创建一个范围为模板的变量? This variable can be shared among the different helpers in the template, but does not exist outside of the template. 此变量可以在模板中的不同帮助程序之间共享,但不存在于模板外部。

In this example below, how can game variable be shared between the 2 templates without repeating its definition? 在下面的这个例子中,如何在不重复定义的情况下在2个模板之间共享game变量? Initializing it using var makes it global which is not what I want. 使用var初始化它会使它全局化,这不是我想要的。 Thank you! 谢谢!

Template.userInfo.game = function() {
    var game = 'Angry Bird';
    return game + ' game';
};

Template.userInfo.score = function() {
    var game = 'Angry Bird';
    return game + ' score';
};

If anyone else stumbles across this and is using Meteor 1.0, here's how you can achieve this. 如果其他人偶然发现并使用Meteor 1.0,那么您可以通过以下方式实现这一目标。

Template.name.onCreated(function(){
    this.data.variableName = "something";
});

Template.name.helpers({
    'helper' : function() {
        return Template.instance().data.variableName;
    }
});

This way the variable is scoped to the instance of the template that was created. 这样,变量的范围限定为创建的模板的实例。 I have a page that uses multiple instance of the same template so this was very useful. 我有一个页面使用相同模板的多个实例,所以这非常有用。

EDIT: 编辑:

So this worked perfectly for templates nested inside another template, but didn't work so well with the parent template. 所以这适用于嵌套在另一个模板中的模板,但是对于父模板不能很好地工作。 The data property wasn't holding values so I did some more research and found this in the example for Template.onCreated they have this.highlightedPicture = new ReactiveVar(null); data属性没有保留值,所以我做了一些更多的研究,并在Template.onCreated的例子中发现了这个 this.highlightedPicture = new ReactiveVar(null); so apparently it's fine to define new properties on your Template instance. 所以显然可以在Template实例上定义新属性。 I tried this in both scenarios and it works great with Template.instance() . 我在两种情况下都尝试过这种方法,而且它适用于Template.instance()

Why don't use 为什么不用

Template.foo.created = function() {
    this._someVariable = "some value"
}

Template.foo.someHelper = function() {
    return this._someVariable
}

Template.foo.events({
    "click #mylink": function(event, tmpl) {
        console.log(tmpl._someVariable)
    }
})

Your private _someVariable is not reactive it serves for options in this case. 在这种情况下,您的私有_someVariable不会为选项提供反应。 But you can wrap a Deps.Dependency() to get a private reactive Template's variables 但是你可以包装一个Deps.Dependency()来获取私有的反应模板的变量

From the docs: http://docs.meteor.com/#namespacing 来自文档: http//docs.meteor.com/#namespacing

Just declare it with a var and it will be file scope. 只需用var声明它,它就是文件范围。 Without the var it will be global scope. 没有var它将是全球范围。

var game = 'Angry Bird'; // File scope.
game2 = 'Angry Bird'; // App scope.

I had some issue to use autorun and variable scope, so maybe it will help someone : 我有一些问题要使用自动运行和变量范围,所以它可能会帮助某人:

Template.foo.someHelper = function() {
     return this._someVariable
}

Template.foo.events({
     "click #mylink": function(event, tmpl) {
          console.log(tmpl._someVariable)
      }
})
Template.foo.onRendered(function() {
      this._someVariable = "some value"
      this.autorun(function(templateInstance) {
            Collection.find({}).fetch(); // Autorun will be executed each time this collection has change (update, delete, insert)
            console.log(templateInstance._someVariable);
      }, this.templateInstance());
});

You could create it as a reactive var in the onCreated then return that variable in a helper. 您可以在onCreated中将其创建为反应式var,然后在帮助器中返回该变量。 Wherever you set that variable it will automatically update the helper value. 无论您set何处set该变量,它都会自动更新辅助值。 Here's an example: 这是一个例子:

Template.foo.onCreated(function() {
    this.yourVar = new ReactiveVar("");
    this.yourVar.set("initValue");
});

Template.foo.helpers({
    yourVar(){
        return Template.instance().yourVar.get();
    }
});

Template.foo.events({
    'click .btn': function (event) {
        template.yourVar.set($(event.target).val());
    }
});

Now you can call {{yourVar}} anywhere in your template and edit it's value as above. 现在,您可以在模板中的任意位置调用{{yourVar}} ,并按上述方式编辑其值。

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

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