Skip to content

Instantly share code, notes, and snippets.

@ldong
Last active August 3, 2017 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ldong/39ea66f9b7be687cab288ac724ed348f to your computer and use it in GitHub Desktop.
Save ldong/39ea66f9b7be687cab288ac724ed348f to your computer and use it in GitHub Desktop.
Meteor Code Snippets Discover Meteor

Discover Meteor

File Structure

/microscope /client /client/templates /client/stylesheets

/server /public

/lib /lib/collections/ /lib/collections/posts.js

/lib are loaded before anything else, any main.* file is loaded after everything else everything else loads in alphabetical order based on the file name

Deployment

meteor up: mup.json and settings.json

Meteor Consoles

meteor shell meteor mongo meteor logs APP_NAME

meteor reset

Meteor Mongo

db.posts.find(); Posts.findOne(); Posts.find().count(); Posts.insert({title: 'Post'}); Posts.find().fetch();

Remove unuse/useless packages

meteor remove autopublish

server/publications.js

Meteor.publish('posts', function(){ return Posts.find(); });

Meteor.publish('posts', function(author){ return Posts.find({author: author}); });

client/main.js

Meteor.subscribe('posts');

Meteor.subscribe('posts', 'bob-smith');

Router

Iron-Router:

  1. Routes
  2. Paths
  3. Segments
  4. Hooks
  5. Filters
  6. Layouts
  7. Controllers

Autorun

Tracker.autorun();

Reactively run every time reactive variables change, i.e. Session.set('varA', 'value1');

Accounts

meteor add ian:accounts-ui-bootstrap-3 meteor add accounts-password

Add the following code to configure the accounts UI to use usernames instead of email addresses:

Accounts.ui.config({ passwordSignupFields: 'USERNAME_ONLY' });

Find users

  1. Meteor.users.findOne();
  2. Meteor.users.find().count();
  3. db.users.count();

Reactivity

Imperative way of observing:

Posts.find().observe({
  added: function(post){

  }  ,
  changed: function(post){

  },
  removed: function(post){

  }
});

Declarative way

Templates.postsList.helpers({
  posts: function(){
    return Posts.find();
  }
});

<template name="postsList">
<ul>
  {{#each posts}}
  <li>{{title}}</li>
  {{/each}}
</ul>
</template>

Computation

Meteor.startup(function(){ Tracker.autorun(function(){ console.log('There are', Posts.find().count(), 'posts'); }); });

// return the _id of the newly created post post._id = Posts.insert({title: 'New Post'});

Security

meteor remove insecure

Posts = new Mongo.Collection('posts');
Posts.allow({
  insert: function(userId, doc){
    return !! userId;
  }
});

Posts.deny({

});

lib/router.js

var requireLogin = function() { if (! Meteor.user()) { this.render('accessDenied'); } else { this.next(); } }

Router.onBeforeAction(requireLogin, {only: 'postSubmit'});

ACCESS DENIED

Ch7.3, Date: 6/9/2016

var requireLogin = function() {
  if (! Meteor.user()) {
    if (Meter.loggingIn()) {
      this.render(this.loadingTemplate);
    } else {
      this.render('accessDenied');
    }
  } else {
    this.next();
  }
}

Router.onBeforeAction('dataNotFound', {
  only: 'postPage'
});

Router.onBeforeAction(requireLogin, {
  only: 'postSubmit'
});

// lib/router.js

screen shot 2016-08-25 at 10 56 23 pm

screen shot 2016-08-26 at 1 01 01 am

screen shot 2016-08-26 at 1 03 06 am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment