// Dependencies import { Meteor } from 'meteor/meteor'; import { Template } from 'meteor/templating'; import { ReactiveVar } from 'meteor/reactive-var'; // Template helpers Template.hello.helpers({ counter() { return Template.instance().counter.get(); } }); // Template events Template.hello.events({ 'click button'(event, instance) { // Increment the counter when button is clicked instance.counter.set(instance.counter.get() + 1); } }); // Template initialization Template.hello.onCreated(function helloOnCreated() { // Set initial counter value this.counter = new ReactiveVar(0); }); // Start Meteor application Meteor.startup(() => { // Code to run on server startup });
// On the server import { Meteor } from 'meteor/meteor'; import { Mongo } from 'meteor/mongo'; // Define database collection export const Tasks = new Mongo.Collection('tasks'); // Publish tasks for client use if (Meteor.isServer) { Meteor.publish('tasks', function tasksPublication() { return Tasks.find(); }); } // On the client import { Meteor } from 'meteor/meteor'; import { Template } from 'meteor/templating'; import { ReactiveDict } from 'meteor/reactive-dict'; import { Tasks } from '../api/tasks.js'; // Template helpers Template.body.helpers({ tasks() { // Subscribe to tasks collection on server Meteor.subscribe('tasks'); // Return all tasks from collection return Tasks.find({}); } }); // Template events Template.body.events({ 'submit .new-task'(event) { event.preventDefault(); // Insert a new task into the collection const target = event.target; const text = target.text.value; Tasks.insert({ text, createdAt: new Date(), owner: Meteor.userId(), username: Meteor.user().username, }); // Clear form input target.text.value = ''; }, }); // Template initialization Template.body.onCreated(function bodyOnCreated() { // Set initial state this.state = new ReactiveDict(); }); // Start Meteor application Meteor.startup(() => { // Code to run on server startup });This example demonstrates the publishing and subscribing of data in Meteor, which allows for real-time updates between the client and server. On the server, a database collection is defined and published for use by the client. On the client, a template subscribes to the published collection and provides helpers and events for accessing and editing the data. Package library: `meteor/meteor`, `mongo`, `templating`, `reactive-dict`