public
Created

Rendering MongoDB documents with Meteor

  • Download Gist
stackover.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<head>
<title>Stackoverflow</title>
</head>
 
<body>
{{> hello}}
</body>
 
<template name="hello">
<ul>
{{#each projects}}
<li>
<p>Name: {{name}}</p>
<p>Personnel:
{{#print personnel}}
<li>Name : {{name}}</li>
<li>Rank : {{rank}}</li>
{{/print}}
</p>
</li>
{{/each}}
</ul>
</template>
stackover.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
Projects = new Meteor.Collection("projects");
Personel = new Meteor.Collection("personel");
if (Meteor.is_client) {
/** Create a helper that will iterate over each id in the object **/
Handlebars.registerHelper('print', function(context, options) {
var ret = "<ul>";
 
for(var i=0, j=context.length; i<j; i++) {
 
var pers = Personel.findOne({_id:context[i]});
if(pers){
ret = ret + options.fn(pers);//Pass the retrieved personel object to the handlerbars function
}
}
ret += "</ul>"
return ret;
});
 
 
Template.hello.projects = function(){
return Projects.find({});
}
 
/**Dummy**/
var one = Personel.insert({name:"Lander",rank:"CEO"});
var two = Personel.insert({name:"Sander",rank:"CEO"});
var three = Personel.insert({name:"Tree",rank:"CEO"});
 
Projects.insert({name:"Project 1",personnel:[one,two]});
}
 
if (Meteor.is_server) {
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.