The Basics of Meteor

Slået op d. - Sidst ændret d.

Meteor is an application platform based on JavaScript and HTML (and a few other languages). It was built with developers in mind, as it simplifies creating prototypes and tackles the hardest parts of deployment and server management. One of the biggest advantages of Meteor is that it is JavaScript-based, and both the front and backend part of the application can be written using the same programming language. The other important feature of meteor is the support for mobile development. It has SDK for both Android and iOS platforms.

Installing Meteor does not require much effort. On OSX and Linux it can be installed using curl and the bash:

        curl | sh

It supports application template generation using the command:

        meteor create PROJECT_NAME

Defining the View

Meteor applications are SPAs (Single Page Applications), having a basic structure defined by the framework and template support. Meteor uses the Blaze template engine/reactive UI library -- this is like Angular.js, Knockout.js, Ember, React or Polymer, but (according to Meteor developers) it's easier to use.

Meteor has three sections, which can be defined in the view: head, body and templates.

 <!-- Latest compiled and minified CSS -->
 <link rel="stylesheet" href="">
  <title>Meteor Contact Cards</title>
  <div class="container">
  	  	<h1>Welcome to Contact Cards using Meteor!</h1>

  	<form class="form-inline" id="new-contact">
	  <div class="form-group">
	    <label class="sr-only" for="first_name">First Name</label>
	    <input type="text" class="form-control" id="first_name" placeholder="Enter First Name">
	  <div class="form-group">
	    <label class="sr-only" for="last_name">Last Name</label>
	    <input type="text" class="form-control" id="last_name" placeholder="Enter Last Name">
	  <div class="form-group">
	    <label class="sr-only" for="birthday">Birthday</label>
	    <input type="text" class="form-control" id="birthday" placeholder="Enter Birthday date">
	  <div class="form-group">
	    <label class="sr-only" for="website">Website</label>
	    <input type="url" class="form-control" id="website" placeholder="http://" value="http://">
	   <div class="form-group">
	    <label class="sr-only" for="email">Email</label>
	    <input type="email" class="form-control" id="email" placeholder="">
	  <button type="submit" class="btn btn-default">Add Contact</button>

	<div class="col-md-12">
	{{#each contacts}}
  	  {{> one_contact}}
  <!-- Latest compiled and minified JavaScript -->
  <script src=""></script>

<template name="one_contact">
  <p> <strong>{{ first_name }} {{ last_name }}</strong> born on <strong>{{ birthday }}</strong> has a website at <strong>{{ website }}</strong> and you can access him/her at <strong>{{ email }}</strong>.</p>

In the view, I have the head section where I defined the title and added a CDN reference for Bootstrap CSS. In the body section, I have the form for entering new contacts and I have the {{ #each contacts }} blaze command, which is a foreach loop, iterating over the contacts collection (this is defined in the JavaScript code below). Inside the #each statement I use the template called one_contact. In Blaze templates can be included using the {{> TEMPLATE_NAME }} syntax.

Defining the logic

For defining the application logic, you'll only need to be familiar with JavaScript. The Template.body.helpers and are important objects inside the meteor framework. In the helpers you define the collections and objects that you need to use on the views. The objects here act like a temporary data store. The name of events object is expressive -- when events occur on the view (HTML page), Meteor checks if there are behaviors defined for the given event.

Contacts = new Mongo.Collection("contacts");

if (Meteor.isClient) {
    contacts : function() {
      return Contacts.find({});
    "submit #new-contact": function(event){

      var first_name =;
      var last_name =;
      var birthday =;
      var website =;
      var email =;

      console.log("Inserting new contact.");
        "first_name": first_name,
        "last_name": last_name,
        "birthday": birthday,
        "website": website,
        "email": email,

      console.log("New contact [" + first_name + " " + last_name + "] was added to the database.") = ""; = ""; = ""; = ""; = "";

      return false;

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup

Meteor offers out of the box support for MongoDB, now how cool is that ? :) Using the new Mongo.Collection(COLLECTION_NAME) syntax it'll open (or create) the collection and you can start to use it. It is important to note, the Contacts mongo collection was created as “global” so it can be accessed both on client and backend side, meteor resolves the data sync between the UI and the server code, you don't have to deal with that. Using the Meteor.isServer and Meteor.isClient variables logic can be split between the front and backend, but more on this later. That was my intro into Meteor...follow-up will come.

You can find the code shown here on GitHub.

Oprettet 5 februar, 2015

Greg Bogdan

Software Engineer, Blogger, Tech Enthusiast

I am a Software Engineer with over 7 years of experience in different domains(ERP, Financial Products and Alerting Systems). My main expertise is .NET, Java, Python and JavaScript. I like technical writing and have good experience in creating tutorials and how to technical articles. I am passionate about technology and I love what I do and I always intend to 100% fulfill the project which I am ...

Næste artikel

Publishing on LinkedIn- Lessons Learnt!