Meteor template.rendered and access to this. Date

I have a template

<template name='order'> {{vendor.name}} </template> 

from

  Template.order.vendor = function () { return {name: 'Chanel', address: 'Paris' }; }; 

When I try to access this.data in

 Template.order.rendered = function () { console.log(this.data); }; 

I get 'undefined'.

What is the correct way to obtain, for example. vendor.name and vendor.address in Template.order.rendered ?

Thanks.

+7
meteor
source share
2 answers

In Template.rendered, this.data corresponds to the data that the template "supplied", either as a parameter or using the {{#with}} construct. the supplier is just a helper function that returns data that is available in the order template but not tied to "this.data". To solve your problem, you have several options:

Defining a parent template and moving the provider helper to this parent, you can also call the supplier order as a parameter or use {{#with block}}

 <template name="parent"> {{> order vendor}} {{#with vendor}} {{> order}} {{/with}} </template> <template name="order"> {{name}} </template> Template.parent.vendor=function(){ return{ name:"Chanel", address:"Paris" }; }; Template.order.rendered=function(){ // this.data == vendor object returned in parent helper console.log(this.data); }; 

You can also register a global helper, eliminating the need for an encapsulating parent template:

 Handlebars.registerHelper("vendor",function(){ return{ name:"Chanel", address:"Paris" }; }); 
+4
source share
 Template.order.rendered = function () { console.log(Template.order.vendor()); } 
0
source share

All Articles