Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Meteor Lesson #2

Meteor Lesson #2

In this presentation can see some Meteor stuffs. Can learn how to install meteor and learn to code some basic things. This presentation based on www.discovermeteor.com

mstamos

May 20, 2015
Tweet

More Decks by mstamos

Other Decks in Education

Transcript

  1. Learning by Creating • MeteorLessons ◦ A list of Lessons

    ▪ Each Lesson has • Title • Date - Time • Location • Mini Description ◦ Registration/Login System ◦ Any logged in user can leave a feedback/comment ◦ Only admin can add new lesson and edit previous meteorlessons.meteor.com github.com/mstamos/meteorlessons
  2. What we will learn... • Install Meteor • Add/Remove Package

    • File Structure • Templates • Storing Data • Publication/Subscription
  3. • Install Meteor by execute the below command into your

    terminal • For Windows download from https://install.meteor.com/windows Getting Started curl https://install.meteor.com | sh
  4. Creating a Simple App > meteor create meteorlessons > cd

    meteorlessons > ls .meteor meteorlessons.css meteorlessons.html meteorlessons.js
  5. Running a Simple App > meteor => Started proxy. =>

    Started your app. => App running at: localhost:3000
  6. What is a package • Small isolated unit which add

    an additional functionality into Meteor app
  7. Five package basic types • The Meteor core itself •

    Regular Meteor packages ◦ Maintained by Meteor Development Group (like accounts-ui) • Third-party packages ◦ you can find them at atmospherejs.com • Local packages • NPM packages
  8. List of app’s packages • Typing the below command >

    meteor list • Move to file ◦ /yourMeteorProject/.meteor/packages meteor-platform autopublish insecure Even Meteor is a package
  9. Adding/Remove a Package Let’s say you want to add Bootstrap

    into your app you just write the command • meteor add twbs:bootstrap ◦ twbs the package author’s username ◦ bootstrap is the package name • meteor remove twbs:bootstrap
  10. File Structure • /server: loaded on the server only •

    /client: loaded on the client only • /tests: not loaded, use it for local test • Everything else runs on both the client and the server • Your static assets (font, images etc) go in the /public
  11. Order of Loading • Files in /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.
  12. Templates - HTML part You can create a new template

    by writing <template name=”templateName”> //something here </template> The HTML part is just a dumb template with placeholders for variables
  13. Spacebars • Meteor’s templating system • Is simply HTML with

    the addition of three things: ◦ inclusions (also called “partials”) ◦ expressions ◦ block helpers
  14. Spacebars Block Helpers Are special tags that control the flow

    of the template, such as {{#each}} … {{/each}} {{#if}} … {{/if}}
  15. Spacebars Inclusions Simply tell Meteor to replace the inclusion with

    the template of the same name {{> templateName }}
  16. Spacebars Expressions Either call a property of the current object,

    or the return value of the template helper as defined in the current template’s manager {{ title }}
  17. Template Helpers Are responsive to do the heavy lifting by

    assigning a value to each variable which is into template file
  18. Live Code Session Let’s download MeteorLessons > git clone https://github.com/mstamos/meteorlessons.git

    > cd meteorlessons Checkout into chapter 2.1 and run the app > git checkout chapter2-1 > meteor
  19. Hot Code Reload Meteor tracks all the files within your

    project directory, and automatically refreshes your browser for you whenever it detects a modification to one of them
  20. Storing Data • The browser’s memory ◦ things like JavaScript

    variables are stored in the browser’s memory ◦ local to the current browser tab • The browser’s storage ◦ using cookies or Local Storage • The server-side database
  21. Collections • What is it? ◦ A special data structure

    that takes care of storing your data in the permanent server-side MongoDB database • Where to put them? ◦ Inside the lib directory to secure that are always defined first ◦ executed both to the client and to the server
  22. Client-Side Collections • called MiniMongo • local, in-browser cache of

    the real Mongo collection • a subset of your data ◦ offers very quick access to this data
  23. Live Code Session Checkout into chapter 3.1 and run the

    app > git checkout chapter3-1 > meteor
  24. Server-Side Collections meteor mongo > db.lessons.insert({title: "A new lesson"}); >

    db.polessonssts.find(); { "_id": ObjectId(".."), "title" : "A new lesson"};
  25. Client-Server Communication > db.posts.find(); {title: "A new post", _id: ObjectId("..")};

    The Mongo Shell ! Posts.findOne(); {title: "A new post", _id: LocalCollection._ObjectID}; First browser console ! Posts.find().count(); 1 ! Posts.insert({title: "A second post"}); 'xxx' ! Posts.find().count(); 2 ! db.posts.find(); {title: "A new post", _id: ObjectId("..")}; {title: "A second post", _id: 'yyy'}; The Mongo Shell ! Posts.find().count(); 2 Second browser console
  26. Publication/Subscription • client subscribes into a real-time data and get

    notifications • three types of notifications ◦ added ◦ changed ◦ removed
  27. Autopublish • A package which is enabled when create a

    Meteor app • The goal of autopublish is to make it very easy started coding your Meteor app • automatically mirroring all data from the server on the client
  28. Subscribing // on the server Meteor.publish('lessons', function(title) { return Lessons.find({done:

    false, title: title}); }); // on the client Meteor.subscribe('lessons', 'First Lesson');
  29. Subscribing - Browser Data // on the client Template.lessonsList.helpers({ lessons:

    function(){ return Lessons.find({title: 'First Lesson', date: '5/5/2015'}); } });
  30. Live Code Session • meteor reset ◦ completely clears out

    the Mongo database Checkout into chapter 3.3 and run the app > git checkout chapter3-3 > meteor
  31. Review • What Meteor is • We create an app

    • Add package • File Structure • Template - Spacebars • Collections • Publish/Subscribe
  32. meteor-exercise-2 Requirements: User should be able to create an account

    • in order to create an account he must insert ◦ username ◦ email ◦ password • User could type a message into an input text box (html tag) and publish it by pressing a submit button • Anyone could write a message ◦ If user is logged in, then his username should be appeared next to his name, otherwise “Anonymous”. Github link: https://github.com/mstamos/meteor-lesson-exercise-2