I'm having the following data structure in my Meteor project:
- Users with a set of list-ids that belong to the user (author)
- Lists that actually contain all the data of the list and a set of user-ids that are allowed to view it (and a owner)
Now I'm trying to publish all Lists of a user to the client with the publish-with-relations-package (Toms version from GitHub). Here is a simple example:
Lists = new Meteor.Collection("lists");
if (Meteor.isClient) {
Deps.autorun(function() {
if (Meteor.userId()) {
Meteor.subscribe("lists");
}
});
Template.hello.greeting = function () {
return "Test";
};
Template.hello.events({
'click input' : function () {
if (typeof console !== 'undefined')
console.log(Lists.find());
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
if ( Meteor.users.find().count() === 0 ) {
var user = Accounts.createUser({ //create new user
username: 'test',
email: 'test@test.com',
password: 'test'
});
//add list to Lists and id of the list to user
var listid = new Meteor.Collection.ObjectID().valueOf();
Meteor.users.update(user._id, {$addToSet : {lists : listid}});
Lists.insert({_id : listid, data : 'content', owner : user._id});
}
});
Meteor.publish('lists', function(id) {
Meteor.publishWithRelations({
handle: this,
collection: Lists,
filter: _id,
mappings: [{
key: 'lists',
collection: Meteor.users
}]
});
});
Meteor.publish("users", function(){
return Meteor.users.find({_id : this.userId});
});
//at the moment everything is allowed
Lists.allow({
insert : function(userID)
{
return true;
},
update : function(userID)
{
return true;
},
remove : function(userID)
{
return true;
}
});
}
Publishing is not working and the Cursor doesn't contain the List element that is initiated.
Any idea how to fix this reactive join for publishing Lists of a certain user? Thanks in advance, any help is appreciated!