Created
April 28, 2013 09:44
-
-
Save jbmoens/5476431 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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._id); // CHANGED here by Ze Jibe | |
}); | |
}); | |
} | |
if (Meteor.isServer) { | |
Meteor.startup(function () { | |
Meteor.publish('parents', function() { | |
return Parents.find(); | |
}); | |
Meteor.publish('kids', function (parentId) { // CHANGED here by Ze Jibe | |
var parent = Parents.findOne({ _id: parentId }); // CHANGED here by Ze Jibe | |
if (!parent) return; // CHANGED here by Ze Jibe | |
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