Dear Meteors,
I am trying to limit the published fields of a collection dynamically depending on “onCreated” hooks from Templates.
My publish function in “server.js”:
Meteor.publish("playerData", function(userName, fieldLimit) {
if (this.userId) {
if(fieldLimit === 'mineBase') {
return playerData.find({
user: userName
},{
fields: {
'user': 1,
'mine.ownSlots': 1,
'mine.scrSlots': 1,
'mine.supSlots': 1,
'mine.minControl': 1,
'mine.maxControl': 1,
}
});
}
else if(fieldLimit === 'improvementsmine') {
return playerData.find({
user: userName
},{
fields: {
'user': 1,
'level': 1,
'XP': 1,
'requiredXP': 1,
'mine.science': 1,
'mine.scrItem.benefit': 1
}
});
}
} else {
this.ready();
}
});
Calls in “onCreated” hooks in “client.js”:
Template.improvements.onCreated(function() {
var inst = this;
var self = Meteor.users.findOne({
_id: Meteor.userId()
}, {
fields: {
menu: 1,
username: 1,
_id: 0
}
});
inst.autorun(function() {
var subsPlayerDataImprovements = inst.subscribe('playerData', self.username, 'improvements'+self.menu);
}
})
});
Template.mineBase.onCreated(function() {
var inst = this;
var self = Meteor.users.findOne({
_id: Meteor.userId()
}, {
fields: {
username: 1,
_id: 0
}
}).username;
inst.autorun(function () {
var subsPlayerDataMineBase = inst.subscribe('playerData', self, 'mineBase');
}
})
});
DDP message for mineBase:
a["{\"msg\":\"added\",\"collection\":\"playerData\",\"id\":\"TBcisBavyTi9KEn6k\",\"fields\":{\"user\":\"bot20\",\"mine\":{\"ownSlots\":3,\"scrSlots\":6,\"supSlots\":2,\"minControl\":0.1,\"maxControl\":10}}}"]
consequent DDP message for improvementsMine:
a["{\"msg\":\"changed\",\"collection\":\"playerData\",\"id\":\"TBcisBavyTi9KEn6k\",\"fields\":{\"level\":0,\"XP\":2,\"requiredXP\":2014}}"]
mineBase.onCreated is invoked first and improvements.onCreated after that. The DDP message for improvementsMine misses two specified fields: “mine.science” and “mine.scrItem.benefit” and I don’t understand why.
Is there some major mistake in how I am trying to achieve this? Mainly I am trying to change my application’s architecture from global subscriptions to template level subscriptions.
Maybe it is relevant that the improvements Template is nested inside the mineBase Template?
Thank you very much in advance!