如何将数据从流星控制器传递给流星助手?

时间:2022-05-29 07:21:17

I am trying to pass data from a controller to a helper:

我试图将数据从控制器传递给帮助器:

My controller:

exampleController = RouteController.extend({

data: function() {

    var a = 13;
    return {
        info: a
    }
},

action: function() {
    this.render('samplePage');
}

});

My helper:

Sample.helpers({
    console.log(info)
});

However, I keep getting a 'undefined' error. Any thoughts?

但是,我不断收到“未定义”错误。有什么想法吗?

1 个解决方案

#1


Setting a data context means that your this is set to whatever you data context is. To access your data context, you would use this or this.something both in your template and in your helpers.

设置数据上下文意味着将此设置为您的数据上下文。要访问您的数据上下文,您可以在模板和帮助程序中使用this或this.something。

On a side note, your helper method syntax is off. According to this, you should be using

另外,您的帮助方法语法已关闭。根据这个,你应该使用

Template.sometemplate.helpers({
  somehelper: function(){
    console.log(this.a);
  }
})

#1


Setting a data context means that your this is set to whatever you data context is. To access your data context, you would use this or this.something both in your template and in your helpers.

设置数据上下文意味着将此设置为您的数据上下文。要访问您的数据上下文,您可以在模板和帮助程序中使用this或this.something。

On a side note, your helper method syntax is off. According to this, you should be using

另外,您的帮助方法语法已关闭。根据这个,你应该使用

Template.sometemplate.helpers({
  somehelper: function(){
    console.log(this.a);
  }
})