Meteorjs - How to track changes () from the client side
I'm new to Meteor. I'm trying to create a proof of concept using Meteor where updates to Mongo oplog are displayed on the browser screen.
I am currently trying to adapt simple-todos for this purpose. I got server log updates for the terminal, but don't know how to bring it to the client browser screen?
if(Meteor.isClient) {
// counter starts at 0
Session.setDefault('counter', 0);
Template.hello.helpers({
counter: function () {
return Session.get('counter');
}
});
Template.hello.events({
'click button': function () {
// increment the counter when button is clicked
Session.set('counter', Session.get('counter') + 1);
}
});
}
if(Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
var Test = new Mongo.Collection('test');
var query = Test.find({});
var init = true;
query.observeChanges({
added: function(id, fields) {
if(!init)
console.log('doc inserted');
},
changed: function(id, fields) {
console.log('doc updated');
},
removed: function() {
console.log('doc removed');
}
});
init = false;
});
}
+3
source to share
1 answer
Define collection for both server and client:
//collection Test for client and server
var Test = new Mongo.Collection('test');
if (Meteor.isClient) {
//subscribe for collection test
Meteor.subscribe('test');
Template.hello.helpers({
test: function() {
var query = Test.find();
query.observeChanges({
added: function(id, fields) {
console.log('doc inserted');
},
changed: function(id, fields) {
console.log('doc updated');
},
removed: function() {
console.log('doc removed');
}
});
return query;
}
});
}
if (Meteor.isServer) {
Meteor.publish('test', function() {
return Test.find();
});
}
For a more complex application, you should structure your application in multiple directories and files. Read about it in the Meteorite Papers .
+4
source to share