Rapidly Prototyping Web Applications With Meteor JS
- Bharani M
What is Meteor JS?
"Meteor is a platform for building top-quality web applications in a fraction of the time".
What is Meteor JS?
- Meteor is built on top of Node JS.
- It sits between your app's database and the user interface and makes sure that both are kept in sync.
-
You use a single language (Javascript) on the server and the client. Meteor lets you share code between the two environments.
- Works best when creating multi-user/realtime apps
If you are familiar with the command line, you’ll know that this command just downloads some files from meteor.com and pipes it to the bin/sh directory.
bin/sh is the location of the script interpreter "Shell", which will run the script.
Installing Meteor
curl https://install.meteor.com | /bin/sh
curl https://install.meteor.com | /bin/sh
Creating A Meteor App
meteor
meteor create contacts
cd contacts
meteor create contacts
cd contacts
This meteor command takes care of loading up all our files and scripts.
Now open up a web browser and navigate to http://localhost:3000/.
Meteor Internals
Packages
Command Line Tool (meteor)
This is similar to rake (ruby) or jake (nodejs)
meteor create my_app
meteor reset
Packages
-
Core Packages - These packages are used in almost every meteor app.
-
Smart Packages - Optional Packages that come bundled with Meteor and you can use for performing specific actions (emails, coffeescript etc)
-
Atmosphere Packages - 3rd party (community-written packages that can be install via meteorite.
-
NPM modules - Does not work out of the box but can be used.
-
Local packages - Files that you put into /packages folder inside your Meteor app.
Using Meteor Smart Packages
meteor list
meteor list --using
meteor add package_name
meteor remove package_name
Command Line Tool
Principles
-
Data on the wire
-
Single Language
-
Database Everywhere
-
Latency Compensation
-
Full Stack Reactivity
-
Embrace the ecosystem
-
Simplicity Equals Productivity
1. Data On The Wire
2. Single Language
3. Database Everywhere
4. Latency Compensation
5. Full Stack Reactivity
6. Embrace The Ecosystem
7. Simplicity = Productivity
Hot Code Reload
Load Order
- Files in the lib directory are loaded first
- Files that match main.* are loaded last
- Files in subdirectories are loaded before files in parent directories (deepest to shallowest)
- Within a directory, files are loaded in alphabetical order.
Separating Client/Server Code
if( Meteor.isClient) {
console.log("client");
}
if( Meteor.isServer ) {
console.log("server");
}
HTML and Templates
HTML files in a Meteor app are different from regular HTML files. There is no <!doctype>, no <script> tags and no css imports.
There are three root level tags -
<head></head><body></body><template></template>
Templates
<div>{{ name }} is {{ age }} years old.</div>
$("div").text(name + "is " + age + " years old.");
Creating A Template
Hello World!
{{greeting}} <template name="hello"><h1>Hello, world</h1>
</template>
<body>
{{> hello }}
</body>
There are three expressions that can be used inside Meteor's templates -
Partials
Partials use the {{> template_name}} syntax. Meteor simply replaces the partial with the template of the same name (in our case postItem).
Expressions
Expressions use the {{ title }} syntax. Meteor replaces this with the value of a property of the current object, or the return value of a template helper.
Block Helpers
Block Helpers are conditional statements to control the flow of template. Examples - {{#each}} ... {{/each}} {{#if}} ... {{/if}}
Handlebars Block Helpers
{{#if currentUser }}
<p>Secret View</p>
{{else}}
<p>Access Denied</p>
{{/if}}
{{#each contacts}}
<p>Name: {{fullName}}</p>
{{/each}}
{{#unless newUser}}
<p>Welcome back</p>
{{else}}
<p>Good to have you here</p>
{{/unless}}
Template Helper Functions
Template.hello.name = function() {
return "Bharani" + "M";
}
Template.hello.name = "Bharani";
<template name="hello">
<h1>Hello, {{ name }}</h1>
</template>
Template Events
Template.hello.events({
“click input” : function(e, t) {
alert(“You clicked the input.”);
}
});
Template Events
Reactivity
-
Session Storage
-
Collections
-
Meteor.user
Reactive contexts - run code as a reactive computation
-
Templates
-
Deps.autorun
Session Storage
Session.set("currentTask", "Learn Meteor"); Session.set("currentPage", "documentationPage");
Session.get("currentTask"); Session.get("currentPage");
Session.equals("currentTask", "Learn Meteor");
Collections + MongoDB
Persistent Data Storage
Every Meteor client includes an in-memory database cache. Server publishes sets of JSON documents and clients subscribes to those sets.
Collections
Contacts = new Meteor.Collection("contacts");
Server Side Collections
Contacts.insert()
Contacts.update()
Contacts.find()
Contacts.remove()
Client Side Collections
Working With Collections
Inserting Records
Contacts.insert({ name: "Bharani" });
Contacts.find();
Contacts.find({ name: "Bharani" });
Contacts.findOne({ name: "Bharani" });
Contacts.update({ name: "Bharani" }, { $set: { name: "John Doe" }});
Contacts.remove({ name: "Bharani" });
MongoDB Crash Course
Alternative to Relational Database (RDBMS) Systems
MongoDB Crash Course
db.collection_name.find({ field1: value1, field2: value2 });
db.collection_name.find({ field1: { $gt: 100 }});
db.collection_name.find({ field1: { $lt: 100 }});
db.collection_name.find({ field1: { $exists: false }});
db.collection_name.find({ field1: value1, $or: [{field2: value2}] });
db.collection_name.find().sort({ name: 1 });
db.collection_name.find().count();
db.collection_name.count({ age: { $gt: 18 }});
db.collection_name.find({}, { age: 1, _id: 0 });
MongoDB Crash Course
db.collection_name.update({ title: "Post #1"}, { $set: { votes: 0 } })
db.collection_name.update({ title: "Post #1"}, { $inc: { votes: 1 } })
db.collection_name.update({ title: "Post #1"}, { $push: { tags: "first_post" } })
db.collection_name.remove( { type : "food" } )
db.collection_name.remove( { type : "food" }, 1 )
MapReduce, Geospatial, Denormalization, Two Phase Commits
Application Structure
Application Structure
- /client - For code that only runs on the client. Meteor pre-processes files in this folder and minifies it before rendering. Place for putting CSS/SCSS/Less files.
-
/server - For code that only runs on the server. Keep your API keys and other private data files in this folder so that it is not accessible by users. (You can also use /private for this)
- /public - Place to store images, robots.txt etc. (CSS files are not considered static assets)
Application Structure
Accounts
accounts-base, accounts-password, accounts-facebook, accounts-github, accounts-google, accounts-meetup, accounts-twitter, or accounts-weibo
accounts-ui
Accounts
meteor add accounts-base accounts-password accounts-ui
{{loginButtons}}
<template name="header">
{{loginButtons}}
</template>
{{#if currentUser }}
<h1>Logged in</h1>
{{/if}}
Meteor.user()
Meteor.userId()
Security
Restricting Read Access
Restricting Write Access
Restricting Read Access -
Publish/Subscribe
meteor remove autopublish
Publish/Subscribe
Meteor.autosubscribe(function() {
Meteor.subscribe("contacts", Meteor.userId());
})
Meteor.publish("contacts", function() {
return Contacts.find({ userId: this.userId });
})
Restricting Write Access -
Allow/Deny
Contacts.allow({
insert: function(userId, doc) {
return !!userId
},
update: function(userId, doc) {
if(userId && doc.userId == userId) return true;
return false;
},
remove: function(userId, doc) {
if(userId && doc.userId == userId) return true;
return false;
}
});
Restricting Write Access -
Meteor Methods
Meteor.call("addOne", {name: "bharani"});
Meteor.methods({
addOne: function(obj) {
Items.insert(obj);
}
})
Deploying
meter deploy contacts-app-demo.meteor.com
meteor bundle contacts.tgz
tar -zxvf bundle.tgz
Meteorite
Meteorite is a Meteor version manager and package manager. It provides an easy way to run different versions of meteor, use non-core packages, and to install packages from the Atmosphere package repository .
npm install -g meteorite
mrt create my_app
mrt add ... (add a package from Atmosphere repository)
Useful Meteorite Packages
-
Router
-
User Roles
-
Admin UI
-
Pagination
-
Animate
-
SCSS
-
Bootstrap 3 / Zurb Foundation
-
Regulate/jQuery Form Validations
-
Momentjs
Learning Resources
Thanks
Rapidly Prototyping Web Applications With Meteor JS
By bharani91
Rapidly Prototyping Web Applications With Meteor JS
- 2,696