Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor template.rendered and this.data access

Tags:

meteor

I have a template

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

rendered with

  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 of getting e.g. vendor.name and vendor.address in Template.order.rendered?

Thank you.

like image 746
joystick Avatar asked Jul 17 '13 10:07

joystick


1 Answers

In Template.rendered, this.data corresponds to the data the template was "fed" with, either as a parameter or using the {{#with}} construct. vendor is just a helper function returning data available in the order template, but not binded to "this.data". SO to solve your problem, you have a number of options :

Defining a parent template and moving the vendor helper to this parent, then you can alternatively call order with vendor as a parameter or use a {{#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"
    };
});
like image 151
saimeunt Avatar answered Oct 13 '22 12:10

saimeunt