Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View dynamicSubscriptions.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<head>
<title>dynamicSubscriptions</title>
</head>
 
<body>
{{> hello}}
</body>
 
<template name="hello">
<h1>Hello World!</h1>
<ul>
{{#each kids}}
<li>{{name}}</li>
{{/each}}
</ul>
</template>
View dynamicSubscriptions.html
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
Kids = new Meteor.Collection('kids');
Parents = new Meteor.Collection('parents');
 
if (Meteor.isClient) {
Template.hello.kids = function () {
return Kids.find();
};
 
Meteor.startup(function(){
Meteor.subscribe('parents');
 
Deps.autorun(function() {
parent = Parents.findOne();
console.log("Found parent", parent);
if (!parent) return;
Meteor.subscribe('kids', parent);
});
});
 
}
 
if (Meteor.isServer) {
Meteor.startup(function () {
Meteor.publish('parents', function() {
return Parents.find();
});
 
Meteor.publish('kids', function (parent) {
return Kids.find({ _id: { $in: parent.childrenIds } });
//return Kids.find();
});
});
 
Meteor.startup( function(){
Parents.remove({});
Kids.remove({});
parentId = Parents.insert({name:'alfred', childrenIds:[]});
console.log("Inserted parentId", parentId);
 
kid1Id = Kids.insert({name:'fred'});
Parents.update(parentId, {$push: {childrenIds:kid1Id}});
console.log("After first push, parent is", Parents.findOne());
 
kid2Id = Kids.insert({name:'george'});
 
Meteor.setTimeout(function() {
Parents.update(parentId, {$push: {childrenIds:kid2Id}});
console.log("After second push, parent is", Parents.findOne());
}, 10*1000);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.