Meteorjs - Passing data context to Template Rendered function?

China☆狼群 提交于 2019-12-13 04:52:58

问题


I have these data context and I want to use JQuery to set the selected value in the tag based on the value.

I am having problem passing the return data from the helpers into the Template.Rendered function.

is there any way of doing that?

Helpers

Template.studentSetting.helpers({
  values: function(){
    return Basics.findOne({userId:Meteor.userId()});
  }
});

Rendered function

Template.studentSetting.rendered = function(){
 //I want to use the "values" helper data here and perform some jquery code based on that?? 
}

回答1:


According to this post, I would advise doing what mpowaga suggests in the thread and just define the helper outside:

var valuesFunc = function () {
  return Basics.findOne({userId:Meteor.userId()});
};

Template.studentSetting.helpers({
  values: valuesFunc
});

Template.studentSetting.onRendered(function(){
  var values = valuesFunc();
});



回答2:


You need to call your studentSettings template with an argument like this:

{{> studentSettings inheritedValues=values}}

You can then access it into your Rendered function like this:

Template.studentSetting.rendered = function(){
 var values = this.data.inheritedValues;
 console.table(values);
}


来源:https://stackoverflow.com/questions/31558725/meteorjs-passing-data-context-to-template-rendered-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!