Skip to content

Instantly share code, notes, and snippets.

View themeteorchef's full-sized avatar

The Meteor Chef themeteorchef

View GitHub Profile
@themeteorchef
themeteorchef / client-signup.coffee
Last active November 29, 2016 12:21
Create user accounts that automatically login without using Accounts.createUser on the client.
Template.signup.events(
'submit form': (e,t) ->
# Prevent form from submitting.
e.preventDefault()
# Grab the user's details.
user =
email: t.find('[name="emailAddress"]').value
password: t.find('[name="password"]').value
@themeteorchef
themeteorchef / list-template.html
Last active July 26, 2016 03:24
Select item select box that matches the passed value.
<template name="example">
<select class="form-control" name="select">
{{#each teams}}
<!--
Here, ../team (../ is a Handlebars convention for traversing template scope) allows us to get the team
value from the parent template (i.e. {{team}}). this._id allows us to get the _id value of the current
item being iterated, or in this example, the _id of the current team. Our UI helper matches the current
team against the current option, if they match, that option is displayed as selected in the select box.
-->
<option {{selectOption ../team this._id}} value="{{_id}}">{{name}}</option>
/*
Meteor allows you to store functions on the server for handling things like data.
This is the server-side component to the Meteor.call() example in add-taco.js.
*/
Meteor.methods({
// Define our method. Note, we're passing an argument for the tacoToInsert value we passed to our Meteor.call() method.
'addTaco': function(tacoName){
// Here we do our insert again.
Tacos.insert({
@themeteorchef
themeteorchef / methods.js
Created January 6, 2015 00:45
Access current userId on the server side.
// Now we can access the current user id via our global variable `currentUserId` in a method.
Meteor.methods({
someUserRelatedMethod: function(){
var user = currentUserId;
console.log(user); // Logs "123456" on the server side. Neat!
}
});
@themeteorchef
themeteorchef / try-catch-example.js
Last active August 29, 2015 14:13
Handling exceptions on the server without a callback
// Accounts.createUser does NOT have a callback on the server, meaning we cannot get an error value and return it. If an error/exception does occur, how do we get it?
Meteor.methods(function(){
createAccount: function(email, password){
try {
// Note: we can only pass parameters to Accounts.createUser on the server, not a callback.
Accounts.createUser({email: email, password: password});
} catch (exception) {
// Instead, using a try catch will allow us to "try" our Accounts.createUser method and if it throws an exception, it will
// "catch" the error, passing the exception to us as an argument.
@themeteorchef
themeteorchef / settings.json
Last active April 24, 2018 11:26
oAuth Configuration Pattern for Meteor
{
"private": {
"oAuth": {
"facebook": {
"development": {
"appId": "xxx",
"secret": "xxx"
},
"staging": {
"appId": "xxx",
@themeteorchef
themeteorchef / calling-method.js
Created May 19, 2015 22:08
Meteor Method Pattern
// On the client-side.
Template.exampleTemplate.events({
'submit form': function(e, t) {
var dataToAddToDatabase = t.find('input.example').value;
// This will work just fine, but delegates the insert to the server.
Meteor.call('processFormData', dataToAddToDatabase, function(error, response){
// Do something with feedback.
});
@themeteorchef
themeteorchef / application-layout.es6.js
Last active August 29, 2015 14:26
Auth in Flow Router?
Template.applicationLayout.helpers({
loggingIn() {
return Meteor.loggingIn();
},
authenticated() {
if ( Meteor.user() ) {
console.log( "We have a user!" );
App.routes.handleUser();
} else {
console.log( "We DO NOT have a user!" );
@themeteorchef
themeteorchef / common-meteor-patterns-in-es2015.md
Created September 8, 2015 13:05
Common Meteor Patterns in ES2015

As we prepare for support of ES2015 in Meteor, it's important that we understand what supporting ES2015 will look like and how it will impact our applications. In this snippet, we'll take a quick look at common Meteor patterns rewritten using ES2015. While this won't be a deep dive into ES2015 itself, it should be enough to help you understand how to bring existing and new applications up to date with the latest version of JavaScript.

Example Code

In order to illustrate usage of ES2016, we'll be using some of the code from the Building Complex Forms recipe.

Template Logic

When we say "Template Logic" in Meteor, we're referring to the JavaScript that's paired with our HTML/Spacebars templates. First, let's look at an example of some template logic written in ES5. This is taken from the [template logic paired

@themeteorchef
themeteorchef / header.html
Created September 9, 2015 15:23
Loading based on all subs elsewhere?
<!-- Then in your template, have a helper bound to that session variable routeLoaded... -->
<template name="header">
<header>
{{#if routeLoaded}}
<p>header contents</p>
{{else}}
{{> loader}}
{{/if}}
</header>