Dart in Action - Pdf 12

MANNING
Chris Buckett
F
OREWORD

BY
Seth Ladd
www.it-ebooks.info
Dart in Action
www.it-ebooks.info

www.it-ebooks.info
Dart in Action
CHRIS BUCKETT
MANNING
Shelter Island
www.it-ebooks.info
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: [email protected]
©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

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 3CLIENT-SIDE DART APPS 209
10 ■ Building a Dart web app 211
11
■ Navigating offline data 237
12
■ Communicating with other systems and languages 258
www.it-ebooks.info
BRIEF CONTENTS
vi
PART 4SERVER-SIDE DART 281
13 ■ Server interaction with files and HTTP 283
14
■ Sending, syncing, and storing data 308
15
■ Concurrency with isolates 331


contents
foreword xv
preface xvii
acknowledgments xix
about this book xxi
about the cover illustration xxv
PART 1INTRODUCING 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

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

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


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

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
www.it-ebooks.info
CONTENTS
xi
9.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


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
www.it-ebooks.info
CONTENTS
xii
12
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


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
www.it-ebooks.info
CONTENTS
xiii
15
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

www.it-ebooks.info
www.it-ebooks.info
xv
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

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!
S
ETH LADD
DEVELOPER ADVOCATE
GOOGLE
www.it-ebooks.info
xvii
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

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.
www.it-ebooks.info
xix
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é

ogy, brings the structure of the server to the front end, and the flexibility, dynamism,
and speed of browser development to the back end.
Whether you’re a novice web developer or are experienced with writing structured
code, this book will help you get up to speed with Dart language concepts. The book
uses an example-based format, with examples throughout each chapter to introduce
new concepts. The text indicates Dart’s similarities to other languages such as Java and
JavaScript, as well as shows its differences.
www.it-ebooks.info
ABOUT THIS BOOK
xxii
Like Java, Dart has great tools; and like JavaScript, Dart doesn’t require a compile
step, which means that with this book you can quickly get ready to start building client
and server Dart applications.
Roadmap
This book is structured to get you working with Dart as quickly as possible. It’s split
into four parts. Part 1 includes overview chapters designed to get you up and running
with Dart:

Chapter 1 provides an overview of the language features and concepts and why
Dart exists. If you’ve ever been exasperated by the lack of typing and documen-
tation that could be encoded in a browser-based language, this chapter will help
you to understand the philosophy behind Dart. This base will give you an idea
of the types of large-scale web apps you can build with Dart.

Chapter 2 discusses the wider Dart ecosystem, including the rich tooling you get
by choosing a structured web-development language created by a market-leading
web company. With the technical resources to concentrate on a whole-developer
experience, rather than just the language, Google has created an
IDE, a custom
Dart browser for rapid development, a server-side virtual machine, and other


In chapter 10, you’ll learn about Dart’s event loop and create a user-interface in
Dart.

Chapter 11 builds on the structure of your app to add browser-based navigation,
persistent client-side storage, and interaction with the JSON data format.

By chapter 12, you’ll be ready to start connecting your app to external systems,
such as external JavaScript and third-party server APIs. Although Dart is tar-
geted at all modern web browsers, in this chapter you’ll also learn how to pack-
age your app for deployment as a Chrome app in Google’s Web Store.
When you reach part 4, you’ll be ready to hook up your app with the server side:

Chapter 13 introduces building a command-line Dart application, accessing the
filesystem, and serving
HTTP data to build a simple file server.

Chapter 14 builds on client-side communication by connecting the client side
to a server-side database and performing two-way communication with Web-
Sockets technology to push data to the client.

In chapter 15, knowing how to interact with the server, you’ll be ready to learn
how Dart achieves concurrency through its system of isolates, a message-passing
threading model that provides a safer means of concurrency than the equiva-
lent in Java or C#. You’ll also use the isolate system to load Dart code dynami-
cally into your running application. This gives you a great basis for building
plug-ins and extensions into your app.
The appendixes provide a concise reference to and examples of the core Dart lan-
guage, giving you a quick guide to Dart’s specific syntax idiosyncrasies and quirks.
Code conventions and downloads

Your purchase of Dart in Action includes free access to a private web forum run by Man-
ning Publications, where you can make comments about the book, ask technical ques-
tions, and receive help from the author and from other users. To access the forum and
subscribe to it, point your web browser at www.manning.com/DartinAction. This page
explains how to get on the forum once you are registered, what kind of help is available,
and the rules of conduct on the forum.
Manning’s commitment to its readers is to provide a venue where a meaningful
dialogue among individual readers, and between readers and the author, can take
place. It’s not a commitment to any specific amount of participation on the part of the
author, whose contribution to the forum remains voluntary (and unpaid). We suggest
you try asking the author some challenging questions, lest his interest stray!
The Author Online forum and archives of previous discussions will be accessible
from the publisher’s website as long as the book is in print.
About the author
Chris Buckett is a technical consultant responsible for delivering enterprise-scale,
web-based business applications. Chris runs the popular Dartwatch.com blog and is an
active contributor to the dartlang mailing list.
www.it-ebooks.info


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status