简体   繁体   English

流星大火renderWithData

[英]Meteor Blaze renderWithData

I'm trying to render a Template via Blaze. 我正在尝试通过Blaze渲染模板。 So normally I call 所以通常我打电话

Blaze.render(Template.xxx,$("#parentnode")[0]);

which works fine. 效果很好。

Now I want to differentiate in the Template xxx who rendered it. 现在,我要区分渲染它的模板xxx。 So I want to pass a variable to the Template which I can use to decide who it was. 所以我想将一个变量传递给Template,我可以用它来确定它是谁。 I tried 我试过了

Blaze.renderWithData(Template.xxx,{test:"value"},$("#parentnode")[0]);

And then tried to access the Data in 然后尝试访问中的数据

 Template.xxx.onRendered(function(){console.log(this.test)});

which logged "this.test is not defined blabla". 哪个记录“ this.test is not blabla”。 How can I pass data correctly and access it? 如何正确传递和访问数据?

Thanks for your answers and have a good one! 感谢您的答复,祝您有一个愉快的答复!

According to meteor docs it is not possible to access context object in onRendered callback. 根据流星文档 ,不可能在onRendered回调中访问上下文对象。 The this keyword refers to the template instance and does not contain any stateful data. this关键字引用模板实例,不包含任何有状态数据。 What you can do is to render the data properties inside DOM nodes (in hidden elements if you don't want to display them). 您可以做的是在DOM节点内渲染数据属性(如果不想显示,则在隐藏元素中)。 You can access the DOM from onRendered function. 您可以从onRendered函数访问DOM。

Template.currentData() is available when the template is rendered so try using Template.currentData() instead of this and it should work: 呈现模板时,Template.currentData()可用,因此请尝试使用Template.currentData()代替它,它应该可以工作:

Template.xxx.rendered = function() {
  var _this = Template.currentData();
  console.log(_this.test)
};

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

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