Meteor in Action
Total Page:16
File Type:pdf, Size:1020Kb
MANNING Stephan Hochhaus Manuel Schoebel FOREWORD BY Matt DeBergalis www.it-ebooks.info Meteor in Action Licensed to Markwww.it-ebooks.info Watson <nordickan@gmail.com> Licensed to Markwww.it-ebooks.info Watson <nordickan@gmail.com> Meteor in Action STEPHAN HOCHHAUS MANUEL CHRISTOPH SCHOEBEL MANNING SHELTER ISLAND Licensed to Markwww.it-ebooks.info Watson <nordickan@gmail.com> For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: orders@manning.com ©2016 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Dan Maharry 20 Baldwin Road Technical development editor Kostas Passadis PO Box 761 Copyeditor: Liz Welch Shelter Island, NY 11964 Proofreader: Barbara Mirecki Technical proofreader: Al Krinker Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781617292477 Printed in the United States of America 12345678910–EBM–201918171615 Licensed to Markwww.it-ebooks.info Watson <nordickan@gmail.com> brief contents PART 1LOOK—A SHOOTING STAR! ...........................................1 1 ■ A better way to build apps 3 2 ■ My fridge! A reactive game 31 PART 23, 2, 1—IMPACT!.........................................................49 3 ■ Working with templates 51 4 ■ Working with data 76 5 ■ Fully reactive editing 105 6 ■ Users, authentications, and permissions 130 7 ■ Exchanging data 153 8 ■ Routing using Iron.Router 176 9 ■ The package system 203 10 ■ Advanced server methods 227 PART 3LEAVING THE CRATER................................................251 11 ■ Building and debugging 253 12 ■ Going into production 281 v Licensed to Markwww.it-ebooks.info Watson <nordickan@gmail.com> Licensed to Markwww.it-ebooks.info Watson <nordickan@gmail.com> contents foreword xiii preface xv acknowledgments xvii about this book xix about the cover illustration xxiii PART 1LOOK—A SHOOTING STAR!................................1 A better way to build apps 3 1 1.1 Introducing Meteor 4 The story behind Meteor 4 ■ The Meteor stack 5 Isomorphic frameworks: full-stack JavaScript 7 Processing in the browser: running on distributed platforms 9 Reactive programming 11 1.2 How Meteor works 12 Core projects 14 ■ Isobuild and the CLI tool 17 Client code vs. server code 17 1.3 Strengths and weaknesses 19 Where Meteor shines 19 ■ Challenges when using Meteor 21 1.4 Creating new applications 24 Setting up a new project 24 ■ Starting the application 25 vii Licensed to Markwww.it-ebooks.info Watson <nordickan@gmail.com> viii CONTENTS 1.5 Dissecting the default project 25 helloWorld.css 26 ■ helloWorld.html 26 ■ helloWorld.js 27 1.6 Summary 29 My fridge! A reactive game 31 2 2.1 Application overview 32 2.2 Initial setup 32 Setting up a new project 33 2.3 Creating a layout 34 Setting the styles 34 ■ Adding the templates 35 2.4 Adding content to the database in real time 38 Storing items in the database 38 ■ Connecting data to templates 40 ■ Adding a defined set of products 43 2.5 Moving items into the fridge 44 Adding jQuery-UI to the project 44 ■ Defining drop targets for items 44 ■ Allowing items to be draggable 47 2.6 Deploying to meteor.com and using the fridge 48 2.7 Summary 48 PART 2 3, 2, 1—IMPACT! .............................................49 Working with templates 51 3 3.1 Introduction to templates 52 3.2 Working with templates 53 The Blaze engine 53 ■ Organizing template files 54 3.3 Creating dynamic HTML templates 55 Double and triple-braced tags (expressions) 55 ■ Inclusion tags (partials) 57 ■ Block tags 60 ■ Helpers 63 3.4 Handling events 68 Template event maps 69 ■ Event propagation 70 Preventing the browser’s default behavior 71 3.5 The template life cycle 72 3.6 Summary 74 Licensed to Markwww.it-ebooks.info Watson <nordickan@gmail.com> CONTENTS ix Working with data 76 4 4.1 Meteor’s default data sources 79 What makes a data source reactive? 80 ■ How reactive data is connected to functions 81 4.2 Building a house-sitter app 82 Setting up templates 84 ■ Connecting to a database and declaring collections 85 4.3 Working with the Session object 86 The Session object 86 ■ Using Session to store selected drop-down values 87 ■ Creating a reactive context using Tracker.autorun 89 4.4 Working with MongoDB collections 89 Querying documents in MongoDB 91 ■ Working with Meteor collections 92 ■ Initializing a collection 93 Querying collections 94 ■ Display collection data in a template 95 ■ Updating data in a collection 98 Inserting new data into a collection 101 ■ Removing data from a collection 102 4.5 Summary 104 Fully reactive editing 105 5 5.1 The reactive editing workflow 106 5.2 Reactive front ends vs. DOM manipulation 107 5.3 Staging changes in a local collection 111 5.4 Displaying collection data within a form 115 Adding array index information to an #each loop 116 5.5 Reactive updates using a local collection 118 Event map for the houseForm template 119 ■ Event map for the plantFieldset template 121 5.6 Implementing a simple notifications system 124 Adding a notifications template 125 ■ Adding a status property 125 ■ Using a Session variable to trigger notifications 127 5.7 Summary 129 Licensed to Markwww.it-ebooks.info Watson <nordickan@gmail.com> x CONTENTS Users, authentications, and permissions 130 6 6.1 Adding users to an application 131 Adding password authentication 132 ■ Registration and password reset 133 ■ Setting up email 137 6.2 Authenticating users with OAuth 141 An introduction to OAuth 141 ■ Integrating Facebook authentication 143 ■ Integrating other OAuth providers 147 6.3 Managing user permissions, roles, and groups 148 Managing permissions with allow/deny 149 6.4 Summary 152 Exchanging data 153 7 7.1 Publications and subscriptions 155 publish() and subscribe() 155 ■ Global subscriptions 157 Template-level subscriptions 158 ■ Parameterizing subscriptions 159 ■ Publishing aggregated data to a client-only collection 162 ■ Turning an aggregation publication into a reactive data source 165 ■ Limiting data visibility by user ID 167 7.2 Meteor methods 169 Removing the insecure package 170 ■ Using methods to write data to collections 171 7.3 Summary 174 Routing using Iron.Router 176 8 8.1 Routing in web applications 177 8.2 Client-side routing 180 Adding Iron.Router 180 ■ Creating your first routes 181 Defining a layout depending on a route 183 ■ Setting the data context depending on a route 187 ■ Data subscriptions with Iron.Router 190 8.3 Advanced routing methods 192 Using named routes and link helpers 192 ■ Working with active routes for better navigation links 194 ■ Waiting for external libraries to load 195 ■ Organizing routes as controllers 196 Extending the route process using hooks 197 ■ Creating an Iron.Router plug-in 199 8.4 Server-side routing with a REST API 200 8.5 Summary 202 Licensed to Markwww.it-ebooks.info Watson <nordickan@gmail.com> CONTENTS xi The package system 203 9 9.1 The foundation of all applications 204 9.2 Using Isopacks 205 Version Solver and semantic versioning 205 ■ Finding packages 206 ■ Adding and removing Isopacks 210 Updating packages 212 9.3 Using npm packages 212 9.4 Creating an Isopack 213 Creating a package 214 ■ Declaring package metadata 215 Adding package functionality 217 ■ Testing Isopacks using tinytest 220 ■ Publishing 224 9.5 Summary 225 Advanced server methods 227 10 10.1 Reintroducing Node.js 227 Synchronous code 228 ■ Asynchronous code 230 10.2 Asynchronous functions using fibers 232 Introducing multitasking to the event loop 232 Binding callbacks to a fiber with wrapAsync 235 Unblocking method calls for a single client 237 Creating fibers with bindEnvironment 238 10.3 Integrating external APIs 240 Making RESTful calls with the http package 241 Using a synchronous method to query an API 242 Using an asynchronous method to call an API 242 10.4 Uploading files to a collection 244 Uploading files to the database 246 10.5 Summary 249 PART 3LEAVING THE CRATER ....................................251 Building and debugging 253 11 11.1 The Meteor build process 254 Build stages 256 ■ Running with the --production flag 259 Load order 260 ■ Adding build stages via packages 262 Adding a custom build stage 264 Licensed to Markwww.it-ebooks.info Watson <nordickan@gmail.com> xii CONTENTS 11.2 Accessing running applications 266 Using the interactive server shell 266 ■ Debugging using node-inspector 267 11.3 Creating browser applications 270 Application configuration using Meteor.settings 270 Building Meteor projects 271 11.4