Skip to content

Instantly share code, notes, and snippets.

@lvbreda
Created July 31, 2012 21:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lvbreda/3220991 to your computer and use it in GitHub Desktop.
Save lvbreda/3220991 to your computer and use it in GitHub Desktop.
Rendering MongoDB documents with Meteor
<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>
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) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment