Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
meteor.js cheatsheet
// Assuming the following directory structure
// 1. Create a collection
// Sets up collection in db and creates a repository to access documents from db
// ----------------------
// -collections/posts.js-
Posts = new Meteor.Collection('posts');
// this code is run on the client and server
// on the server, it creates the collection in mongodb and acts as a repository
// on the client it creates a repository which accesses "minimongo" which will be populated by a subscription later
// 2. Make some fixture data
// Figure out what your data should look like, write some json objects and insert them into the db when it's empty
// ----------------------
// -server/fixtures.js-
if (Posts.find().count() === 0) {
title: 'Introducing Telescope',
author: 'Sacha Greif',
url: '',
details: "Long paragraph that we don't want to show on the list page, so only show in detail view"
title: 'Meteor',
author: 'Tom Coleman',
url: '',
details: "Long paragraph that we don't want to show on the list page, so only show in detail view"
title: 'The Meteor Book',
author: 'Tom Coleman',
url: '',
details: "Long paragraph that we don't want to show on the list page, so only show in detail view"
// 3. Create a publication
// From the server, you need to publish the data the clients should have access to
// No REST in Meteor, need to start thinking in terms of publications and subscriptions
// ----------------
// -server/publications.js-
Meteor.publish('allPosts', function() {
return Posts.find();
// 4. Subscibe to publications
// ----------------
// -client/main.js- *main.js is loaded after everything else
// 5. Create basic templates and managers
// There are several basic templates we should make for each collection
// 1) Collection View
// -client/posts/posts.html
<template name="posts">
{{#each posts}}
{{> postItem}}
// -client/posts/posts.js
posts: function() {
return Posts.find().map(function(post) {
// edit each item client side here if you want
// 2) Collection Item View (for collection)
// -client/posts/postItem.html
<template name="postItem">
<a href="{{url}}">{{title}}</a>
// 3) Detail View
<template name="postDetail">
<a href="{{url}}">{{title}}</a>
// 4) Edit view
// ** should be able to use as create as well
// ** maybe check out x-editable on detail view and set "editMode" session variable?
// ----------------
<template name="postEdit">
<input type="text" name="title" value="{{title}}" placeholder="Enter Title" />
<input type="text" name="url" value="{{url}}" placeholder="Enter URL" />
<input type="text" name="author" value="{{author}}" placeholder="Enter Author" />
<input type="text" name="details" value="{{details}}" placeholder="Enter Details" />
<input type="submit" value="Submit" />
"submit form": function(e) {
var post = {
url: $('[name=url]').val(),
title: $('[name=title]').val(),
author: $('[name=author]').val(),
details: $('[name=details]').val(),
}'editPost', post, function(error, id) {
if (error) {
} else {'postDetail', id);
// in collections/posts.js add
post: function(postAttributes) {
var user = Meteor.user(),
postWithSameLink = Posts.findOne({url: postAttributes.url});
// ensure the user is logged in
if (!user)
throw new Meteor.Error(401, "You need to login to post new stories");
// ensure the post has a title
if (!postAttributes.title)
throw new Meteor.Error(422, 'Please fill in a headline');
// check that there are no previous posts with the same link
if (postAttributes.url && postWithSameLink) {
throw new Meteor.Error(302,
'This link has already been posted',
// pick out the whitelisted keys
var post = _.extend(_.pick(postAttributes, 'url', 'title', 'message'), {
userId: user._id,
author: user.username,
submitted: new Date().getTime(),
commentsCount: 0,
upvoters: [], votes: 0
var postId = Posts.insert(post);
return postId;
// more publication examples
Meteor.publish('newPosts', function(limit) {
return Posts.find({}, {sort: {submitted: -1}, limit: limit});
Meteor.publish('singlePost', function(id) {
return id && Posts.find(id);
//related to post
Meteor.publish('comments', function(postId) {
return Comments.find({postId: postId});
// all that belong to user
Meteor.publish('notifications', function() {
return Notifications.find({userId: this.userId});
// complex example, partial publication, partial properties
Meteor.publish('allPosts', function(){
return Posts.find({'author':'Tom'}, {fields: {
date: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment