meteor-reactive-publish
meteor-reactive-publish copied to clipboard
Publish re-run on client collections
Hi pal,
Im getting something wired here, the re-run executes twice the publish added method (which is logical) but in the re-run the record already exist and we need to execute the changed method to update the client record ... im right ?? but how can intercept that ??
If you give me some advice ... it will be welcome :+1:
PD, im in meteor world few weeks ago (maybe im doing something wrong?)
Here the code:
Meteor.reactivePublish('stats', function (sod) {
check(sod, Number);
var self = this;
var userId = this.userId;
if (userId) {
var user = Meteor.users.findOne({
_id: this.userId
}, {
reactive: true
});
var stats = {
count: 0,
};
var initializing = true;
var items = user.items;
if (items) {
var query = Items.find({
_id: {$in: items},
timestamp: {$gt: sod},
}, {
sort: { timestamp: -1 }
});
var handle = query.observe({
added: function (document) {
stats.count += 1;
if (!initializing) {
// update record
self.changed('stats', userId, status);
}
}
});
// create initial record
self.added('stats', userId, stats);
// done
initializing = false;
self.ready();
self.onStop(function () {
handle.stop();
});
} else {
// pass
self.ready();
}
} else {
// pass
self.ready();
}
});
thks !!
Probably the reactivePublish method must be removed the records added before the re-run.
btw, im using the old school rules (plain publish method with two observers) and all goes flawlessly (but with a lot of code :open_mouth: )