M A N N I N G Stephan Hochhaus Manuel Schoebel F Matt DeBergalis OREWORD BY www.it-ebooks.info Meteor in Action Licensed to Marwk wWwa.tist-oenb o<[email protected]> Licensed to Marwk wWwa.tist-oenb o<[email protected]> Meteor in Action STEPHAN HOCHHAUS MANUEL CHRISTOPH SCHOEBEL MANNING SHELTER ISLAND Licensed to Marwk wWwa.tist-oenb o<[email protected]> 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: [email protected] ©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 1 2 3 4 5 6 7 8 9 10 – EBM – 20 19 18 17 16 15 Licensed to Marwk wWwa.tist-oenb o<[email protected]> brief contents PART 1 LOOK—A SHOOTING STAR! ...........................................1 1 ■ A better way to build apps 3 2 ■ My fridge! A reactive game 31 PART 2 3, 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 3 LEAVING THE CRATER................................................251 11 ■ Building and debugging 253 12 ■ Going into production 281 v Licensed to Marwk wWwa.tist-oenb o<[email protected]> Licensed to Marwk wWwa.tist-oenb o<[email protected]> contents foreword xiii preface xv acknowledgments xvii about this book xix about the cover illustration xxiii PART 1 LOOK—A SHOOTING STAR!................................1 1 A better way to build apps 3 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 Marwk wWwa.tist-oenb o<[email protected]> viii CONTENTS 1.5 Dissecting the default project 25 helloWorld.css 26 ■ helloWorld.html 26 ■ helloWorld.js 27 1.6 Summary 29 2 My fridge! A reactive game 31 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 3 Working with templates 51 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 Marwk wWwa.tist-oenb o<[email protected]> CONTENTS ix 4 Working with data 76 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 5 Fully reactive editing 105 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 Marwk wWwa.tist-oenb o<[email protected]>
Description: