Dart in Action (Chris Buckett) (z-library.sk, 1lib.sk, z-lib.sk)

Author: Chris Buckett

代码

Summary Dart in Action introduces Google's Dart language and provides techniques and examples showing how to use it as a viable replacement for Java and JavaScript in browser-based desktop and mobile applications. It begins with a rapid overview of Dart language and tools, including features like interacting with the browser, optional typing, classes, libraries, and concurrency with isolates. After you master the core concepts, you'll move on to running Dart on the server and creating single page HTML5 web applications. About the Technology Dart is a web programming language developed by Google. It has modern OO features, just like Java or C#, while keeping JavaScript's dynamic and functional characteristics. Dart applications are "transpiled" to JavaScript, and they run natively in Dart-enabled browsers. With production-quality libraries and tools, Dart operates on both the client and the server for a consistent development process. About this Book Dart in Action introduces the Dart language and teaches you to use it in browser-based, desktop, and mobile applications. Not just a language tutorial, this book gets quickly into the nitty-gritty of using Dart. Most questions that pop up while you're reading are answered on the spot! OO newbies will appreciate the gentle pace in the early chapters. Later chapters take a test-first approach and encourage you to try Dart hands-on. To benefit from this book you'll need experience with HTML and JavaScript?a Java or C# background is helpful but not required. Purchase of the print book comes with an offer of a free PDF, ePub, and Kindle eBook from Manning. Also available is all code from the book. What's Inside Dart from the ground up Numerous code samples and diagrams Creating single-page web apps Transitioning from Java, C#, or JavaScript Running Dart in the browser and on the server About the Author Chris Buckett builds enterprise-scale web applications. He runs Dartwatch.com and is an active contributor to the da

📄 File Format: PDF
💾 File Size: 15.6 MB
8
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
M A N N I N G Chris Buckett FOREWORD BY Seth Ladd
📄 Page 2
Dart in Action
📄 Page 3
(This page has no text content)
📄 Page 4
Dart in Action CHRIS BUCKETT M A N N I N G Shelter Island
📄 Page 5
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 261 Shelter Island, NY 11964 Email: orders@manning.com ©2013 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. Development editor: Susanna Kline Manning Publications Co. Technical proofreader: John Evans 20 Baldwin Road Copyeditor: Tiffany Taylor PO Box 261 Proofreader: Toma Mulligan Shelter Island, NY 11964 Typesetter: Gordan Salinovic Cover designer: Marija Tudor ISBN 9781617290862 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 17 16 15 14 13 12
📄 Page 6
brief contents PART 1 INTRODUCING DART .........................................................1 1 ■ Hello Dart 3 2 ■ “Hello World” with Dart tools 24 3 ■ Building and testing your own Dart app 40 PART 2 CORE DART.................................................................. 69 4 ■ Functional first-class functions and closures 71 5 ■ Understanding libraries and privacy 94 6 ■ Constructing classes and interfaces 119 7 ■ Extending classes and interfaces 138 8 ■ Collections of richer classes 158 9 ■ Asynchronous programming with callbacks and futures 183 PART 3 CLIENT-SIDE DART APPS.................................................209 10 ■ Building a Dart web app 211 11 ■ Navigating offline data 237 12 ■ Communicating with other systems and languages 258v
📄 Page 7
BRIEF CONTENTSviPART 4 SERVER-SIDE DART ........................................................281 13 ■ Server interaction with files and HTTP 283 14 ■ Sending, syncing, and storing data 308 15 ■ Concurrency with isolates 331
📄 Page 8
contents foreword xv preface xvii acknowledgments xix about this book xxi about the cover illustration xxv PART 1 INTRODUCING DART.............................................1 1 Hello Dart 3 1.1 What is Dart? 3 A familiar syntax to help language adoption 5 ■ Single-page application architecture 6 1.2 A look at the Dart language 7 String interpolation 7 ■ Optional types in action 9 Traditional class-based structure 10 ■ Implied interface definitions 11 ■ Factory constructors to provide default implementations 12 ■ Libraries and scope 13 ■ Functions as first-class objects 16 ■ Concurrency with isolates 17 1.3 Web programming with Dart 18 dart:html: a cleaner DOM library for the browser 18 ■ Dart and HTML5 19vii
📄 Page 9
CONTENTSviii1.4 The Dart tool ecosystem 20 The Dart Editor 20 ■ Dart virtual machine 21 ■ Dartium 21 dart2js: the Dart-to-JavaScript converter 22 ■ Pub for package management 22 1.5 Summary 23 2 “Hello World” with Dart tools 24 2.1 The command-line Dart VM 25 2.2 “Hello World” with the Dart Editor 26 Exploring the Dart Editor tools 27 ■ The relationship between Dart and HTML files 30 ■ Running “Hello World” with Dartium 30 Using dart2js to convert to JavaScript 32 ■ Generating documentation with dartdoc 34 ■ Debugging Dart with breakpoints 34 2.3 Importing libraries to update the browser UI 35 Importing Dart libraries 36 ■ Accessing DOM elements with dart:html 37 ■ Dynamically adding new elements to the page 38 2.4 Summary 39 3 Building and testing your own Dart app 40 3.1 Building a UI with dart:html 41 Entry-point HTML 42 ■ Creating dart:html elements 42 Creating a new Element from HTML snippets 44 ■ Creating elements by tag name 45 ■ Adding elements to an HTML document 46 3.2 Building interactivity with browser events 49 Adding the PackList item from a button click 49 ■ Event handling with Dart’s flexible function syntax 50 ■ Responding to dart:html browser events 52 ■ Refactoring the event listener for reuse 53 Querying HTML elements in dart:html 54 3.3 Wrapping structure and functionality with classes 56 Dart classes are familiar 57 ■ Constructing the PackItem class 57 ■ Wrapping functionality with property getters and setters 59 3.4 Unit-testing the code 62 Creating unit tests 64 ■ Defining test expectations 64 Creating a custom matcher 66 3.5 Summary 67
📄 Page 10
CONTENTS ixPART 2 CORE DART ......................................................69 4 Functional first-class functions and closures 71 4.1 Examining Dart functions 72 Function return types and the return keyword 74 ■ Providing input with function parameters 77 4.2 Using first-class functions 82 Local function declarations 83 ■ Defining strong function types 88 4.3 Closures 91 4.4 Summary 93 5 Understanding libraries and privacy 94 5.1 Defining and importing libraries in your code 95 Defining a library with the library keyword 96 ■ Importing libraries with import 98 5.2 Hiding functionality with library privacy 103 Using privacy in classes 105 ■ Using private functions in libraries 109 5.3 Organizing library source code 110 Using the part and part of keywords 111 5.4 Packaging your libraries 114 5.5 Scripts are runnable libraries 116 5.6 Summary 118 6 Constructing classes and interfaces 119 6.1 Defining a simple class 120 Coding against a class’s interface 121 ■ Formalizing interfaces with explicit interface definitions 123 ■ Using multiple interfaces 124 ■ Declaring property getters and setters 125 6.2 Constructing classes and interfaces 126 Constructing class instances 127 ■ Designing and using classes with multiple constructors 128 ■ Using factory constructors to create instances of abstract classes 129 ■ Reusing objects with factory constructors 130 ■ Using static methods and properties with factory constructors 132
📄 Page 11
CONTENTSx6.3 Creating constant classes with final, unchanging variables 134 Final values and properties 134 ■ The constructor initialization block 134 ■ Using the const keyword to create a const constructor 135 6.4 Summary 136 7 Extending classes and interfaces 138 7.1 Extending classes with inheritance 139 Class inheritance 140 ■ Inheriting constructors 142 Overriding methods and properties 143 ■ Including abstract classes in a class hierarchy 144 7.2 Everything is an object 147 Testing the “is-an” relationship with Object 147 ■ Using the “is- an” Object relationship 149 ■ Using toString() functionality inherited from the base Object class 150 ■ Intercepting noSuchMethod() calls 151 ■ Other default functionality of the Object class 153 7.3 Introducing the dynamic type 154 Using the dynamic type annotation 156 7.4 Summary 156 8 Collections of richer classes 158 8.1 Working with collections of data 159 Collections of objects 160 ■ Using the concrete implementations of the Collection interface 164 ■ Making collections specific with generics 166 ■ Storing lists of key/value pairs with generic maps 170 8.2 Building your own generic classes 173 Defining a generic class 173 ■ Using your custom generic class 175 ■ Restricting the types that can be used as placeholders 175 8.3 Operator overloading 176 Overloading comparison operators 177 ■ Surprising use for operator overloading 179 ■ Overloading indexer operators 179 8.4 Summary 182 9 Asynchronous programming with callbacks and futures 183 9.1 Why web apps should be asynchronous 185 Modifying your app to be asynchronous 187
📄 Page 12
CONTENTS xi9.2 Using callbacks with async programming 190 Adding async callbacks to Dart Lottery 192 ■ Ensuring that all async callbacks are complete before continuing 193 ■ Nesting callbacks to enforce async execution order 195 9.3 Introducing the Future and Completer pair 197 Passing around future values 198 ■ Ordering async calls by chaining futures 199 ■ Waiting for all futures to complete 200 Transforming nonfuture values into futures 202 9.4 Unit-testing async APIs 203 Testing async callback functions 205 ■ Testing future values 205 9.5 Summary 207 PART 3 CLIENT-SIDE DART APPS ....................................209 10 Building a Dart web app 211 10.1 A single-page web app design 212 Introducing DartExpense 212 ■ Dart application structure 216 Dart app execution flow 217 10.2 Building a UI with dart:html 220 Understanding the Element interface 220 ■ Element constructors in action 223 ■ Building interaction with views and elements 225 ■ Building a simple generic grid 228 10.3 Handling browser events with dart:html 231 Managing browser event flow 232 ■ Common event types 235 10.4 Summary 236 11 Navigating offline data 237 11.1 Integrating navigation with the browser 239 Using pushState() to add items to the browser history 239 Listening for popState events 241 11.2 Using browser cookies to enhance user experience 243 Storing data in a cookie 244 ■ Reading data from a cookie 245 11.3 Persisting data offline with Web Storage 247 Converting Dart objects to JSON strings 248 ■ Converting JSON strings to Dart objects 252 ■ Storing data in browser web storage 253 11.4 Summary 257
📄 Page 13
CONTENTSxii12 Communicating with other systems and languages 258 12.1 Communicating with JavaScript 259 Sending data from Dart to JavaScript 262 ■ Receiving data in JavaScript sent from Dart 263 ■ Sending data from JavaScript to Dart 265 12.2 Communicating with external servers 268 Understanding the same-origin security restrictions 269 Using JSONP to request data from external servers 270 12.3 Building installable, server-less browser apps 273 Using AppCache to run applications offline 273 ■ Packaging your app as a Chrome web app 277 12.4 Summary 280 PART 4 SERVER-SIDE DART ...........................................281 13 Server interaction with files and HTTP 283 13.1 Running server-side Dart scripts 284 Accessing command-line arguments 287 ■ Accessing files and folders with dart:io 288 13.2 Serving browser HTTP requests 294 Using the Dart HttpServer 295 ■ Serving static files over HTTP 297 13.3 Serving clients with a RESTful API 299 Sending a directory list as JSON data 301 ■ Sending the file content as JSON data 302 ■ Adding the client-side user interface 303 13.4 Summary 307 14 Sending, syncing, and storing data 308 14.1 Serving DartExpense from the server 309 14.2 Using web sockets for two-way communication 310 Connecting web sockets on the client side 311 ■ Handling web socket connections on the server 312 ■ Using web sockets for cross- browser synchronization 315 14.3 Storing data with HttpClient and CouchDB 320 A quick CouchDB primer 321 ■ Sharing the Expense model class between client and server 324 ■ Adding server support for data persistence 324 14.4 Summary 329
📄 Page 14
CONTENTS xiii15 Concurrency with isolates 331 15.1 Using isolates as units of work 332 Creating an isolate 332 ■ One-way isolate communication 335 Two-way isolate communication 338 15.2 Loading code dynamically 341 Spawning an isolate from a filename 343 ■ Defining a dynamic source file 344 15.3 Spawning multiple workers 345 15.4 Summary 350 appendix A Core language reference 351 appendix B Defining classes and libraries 371 index 386
📄 Page 15
(This page has no text content)
📄 Page 16
foreword When I heard that we were starting work on Dart, a structured and scalable language with a fast virtual machine, a powerful editor, and a compiler to JavaScript, at first I didn’t believe it. “Could this be the project to make web programming easier for developers like me?” I hopefully wondered. Coming from a structured language back- ground, and used to powerful developer tools, I’d been waiting for a more productive way to build larger modern web apps. The Dart project sounded like just what I was looking for. I grew up on object-oriented languages like C++, Java, and Ruby as I first built interactive websites and then later rich client-side web apps. I learned to be pro- ductive with classes, objects, and modular code. I appreciated IDEs for their analysis, refactoring, and navigation capabilities because they helped me write more complex, larger applications. Life was great. Looking for a new opportunity, I was lucky enough to get a job working with the Chrome team. For the first time, I learned how to exploit the modern browser, and I dove into the many HTML5 features. The modern web evolves very quickly and reaches so many people that it’s an exciting place to be. Life was even better. Although I loved the iterative and fast-paced nature of web development, I was missing my structured languages and helpful tools. I wanted a way to build for modern browsers with IDEs that could perform code completion, languages that had real classes, and more. So when I heard about Dart, I jumped at the opportunity to help out. Build for the most exciting platform with a development experience that I’m familiar and produc- tive with? You bet!xv
📄 Page 17
FOREWORDxvi I wasn’t the only developer who immediately joined the fun. The author of this book, Chris Buckett, is one of our earliest adopters of Dart. He started the Dartwatch blog on the day that Google announced Dart, and it’s still going strong. Chris has been with the project since the beginning, so it’s only natural that he is one of the first to write a book to help other developers learn Dart. Chris is some sort of super author, for he has been able to write this book as the project was going through numerous changes to its libraries and language. He’s done a great job covering the many different aspects and features of the Dart project. I especially enjoyed his numerous examples of not only the core language features, but also the more advanced HTML5 features. Chris embraces the single-page app and shows how to use Dart to build modern browser-based apps. You’ll even learn how to program server-side Dart with this book! After a year of hard work, tens of thousands of commits, thousands of bugs, and great community feedback, the dream of structured web programming is a reality. Although Dart isn’t done yet, thanks to Chris’s book, together we can have fun build- ing great apps for the modern web. Enjoy! SETH LADD DEVELOPER ADVOCATE GOOGLE
📄 Page 18
preface In October 2011, rumor became reality when Google released a new language aimed at developing complex, Google-scale web applications. An internal Google email titled “Future of JavaScript” had appeared on the web a month earlier, indicating that a lan- guage, possibly to be known as Dash, was undergoing development within Google, with the aim of being a better language for the web than JavaScript. Born out of frustration with the slow progress in evolving JavaScript, partly caused by the numerous interested parties and committees, this new language aimed to be everything JavaScript could be if it were invented now. Its key goal was to “maintain the dynamic nature of JavaScript, but have a better performance profile and be amenable to tooling for large projects.” It would also be able to cross-compile to JavaScript. This language was released as a tech- nical preview to the wider world and given the name Dart. I had just come out the back of a major GWT project at my employer, creating a bespoke document-referencing application designed for touch screens that would be deployed in non-computer-friendly environments. Google Web Toolkit (GWT) is a technology that Google created for cross-compiling Java to JavaScript. GWT lets devel- opers benefit from the structure, type-safety, and tooling provided by Java, while still being able to target browsers natively without requiring plug-ins such as Flash or Sil- verlight. Having spent the last two years writing GWT code and coordinating develop- ers across three countries, I knew the value of being able to use tooling to validate code at integration points—something that was lacking when trying to achieve the same with JavaScript. The ability to reuse code on both the client and the server also appealed to me—I had seen the benefit. xvii
📄 Page 19
PREFACExviii Keen to know more about this new Dart language, I read all the documentation that was available. At the time, this consisted of the source code, some sample proj- ects, and the language specification. It seemed that if I were to make the effort of get- ting the knowledge into my head, it would be worth sharing with the wider community through blog posts. I started the Dartwatch blog and shared a series of simple descriptions of how to achieve common tasks in Dart, such as how to organize a project, how to create classes, and how to interact with the browser. One thing led to another, and I was approached by Manning about the possibility of writing a book on Dart. Just over a year later, the result is in print. Over the last year, Dart has had time to mature, and its developers have been lis- tening and responding to feedback. Dart’s Milestone 1 release is imminent, and there have been many changes to the original language specification as a result of real- world use by the language’s early adopters. A community of these early adopters has also been creating tools and libraries such as database drivers, 2D and 3D graphics libraries, and MVC frameworks, many of which can be found on GitHub or on the Dartwatch website. Dart Milestone 1 is a major achievement and gives Dart developers the chance to build on the core Dart language to create a great set of libraries and APIs to turn Dart into the “batteries included” language that the team at Google envisages. Every day, Dart improves; and thanks to its open source nature, you can watch (and even contrib- ute to) the commits by many developers into the Dart source code repository. I hope that this book helps you build great Dart apps.
📄 Page 20
acknowledgments It turns out that writing a book isn’t as straightforward as I first thought, and without the guidance and effort of the all who were involved at Manning, it’s unlikely you would be reading this book today. Thanks to Michael Stephens for setting me on this path in the first place; it’s been a fun project. Many people behind the scenes at Man- ning have contributed by proofreading, editing, preparing images, and performing the myriad other tasks that go into producing a book such as this—thank you all. A special mention must also go to two people at Manning. First, thanks to Bert Bates, whose mentoring in the early days showed me how to turn what could other- wise have been a dry reference manual into something that is more pleasing to read. In the back of my mind when writing each chapter was the mantra, “Tell Bert why he should care about this subject…” Second, thanks to my editor, Susanna Kline, who kept each chapter focused and helped keep me motivated and on schedule for the best part of a year. Dart has a vibrant developer community centered around the dartlang mailing list and Google+. From that community, John Evans and Kevin Moore deserve thanks for their technical proofreading of the subject matter, along with Adam Singer, Matthew Butler, and Ladislav Thon, whose contributions are always welcome. Also from the developer community, thanks to all those readers who provided valu- able feedback by reviewing the book at its various stages of development: André Roberge, Carl Taswell, Chad Davis, Craig Lancaster, Dylan Scott, Glenn Stokol, Jon Skeet, Olivier Nouguier, Rick Goff, Rodney Bollinger, Rokesh Jankie, Steve Pretty, Terry Birch, and Willhelm Lehman.xix
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now

Recommended for You

Loading recommended books...
Failed to load, please try again later
Back to List