This code has a collection in /lib/collection.js “HeaderLabelCol” with a server method which inserts a new document per userId, another server code updates this document’s field named headerLabel and I wish to re publish the document so that this client’s template gets updated with the new value of the headerLabel field.
I don’t understand fully what this code is doing but able to more or less understand most of it. I need your help. Thanks
Template.header.helpers({
headerLabel: function () {
var userId = Meteor.userId();
if (userId) {
Meteor.call('getHeaderLabel', userId, function (err, res) {
if (!err) {
Session.set('headerLabel', res);
}
});
return Session.get('headerLabel');
} else {
return 'Please login'
}
}
});
// lib/collection.js
HeaderLabelCol = new Mongo.Collection(‘headerLabelCol’);
// server.js
Meteor.publish('headerLabelCol', function (userId) {
var self = this;
var handle = HeaderLabelCol.find({headerLabel: {$in: [self.userId]}}).observeChanges({
changed: function (id, fields) {
self.update('headerLabelCol', oldValue, fields.headerLabel.value);
}
});
self.ready();
self.onStop(function () {
handle.stop();
});
});
getHeaderLabel: function (userId) {
if (userId) {
var result = HeaderLabelCol.findOne({userId: userId});
if (result) {
return result.headerLabel;
}
}
}