Oliver
Oliver

Reputation: 2212

Meteor- pass data to nested templates

Meteor newbie here.

I have a template for a home page. The home pages has several "day"s in it, which each have "task"s. I want to display the appropriate task in the day that it belongs to, but I don't know how to do this.

I also only want to retrieve the relevant tasks with one database query if possible (ie all tasks within two weeks).

I found a couple other questions possibly related to this including this and this but I can't discern any useful related info.

I have a collection for tasks, and as part of the home page I retrieve a two week span of tasks. Then I sort them into day buckets.

buckets = [[...], [...], [...], ... ] # array of arrays of task objects

Now I don't know what to do. In the home template, I think I can do

Template.home.helpers(
    tasks: ()->
        #return buckets, defined above
)
(home.coffee)

<template name="home">
    {{#each tasks}}
         {{> day}}
    {{/each}}
</template>
(home.html)

to iterate over the day buckets, but how do I access the task objects from each day template?

<template name="day">
    {{#each ???}}
        {{> task}}
    {{/each}}
</template>

<template name="task">
    {{name}}
</template>

How can I access the data from the current iteration of the each loop from the parent template? Am I structuring this incorrectly? Should I just make separate db calls for every day?

Upvotes: 2

Views: 3233

Answers (1)

Hubert OG
Hubert OG

Reputation: 19544

This should do the trick:

<template name="day">
    {{#each this}}
        {{> task}}
    {{/each}}
</template>

Edit: this is the incorrect original answer.

Let task have fields called name, important and dueToday. Then you may write:

<template name="day">
  {{name}}
</template>

Or, if you insist:

<template name="day">
  {{this.name}}
</template>

Also:

Template.day.isItUrgent = function() {
  return this.data.important && this.data.dueToday;
};

Upvotes: 3

Related Questions