Exploring Dojo with Firebug 21 Summary 31
Total Page:16
File Type:pdf, Size:1020Kb
Dojo The Definitive Guide Other resources from O’Reilly Related titles Adding Ajax JavaScript: The Definitive Ajax: The Definitive Guide Guide CSS Cookbook™ JavaScript: The Good Parts CSS: The Definitive Guide Learning JavaScript Dynamic HTML: Painting the Web The Definitive Reference oreilly.com oreilly.com is more than a complete catalog of O’Reilly books. You’ll also find links to news, events, articles, weblogs, sample chapters, and code examples. oreillynet.com is the essential portal for developers interested in open and emerging technologies, including new platforms, pro- gramming languages, and operating systems. Conferences O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in document- ing the latest tools and systems, translating the innovator’s knowledge into useful skills forthose in the trenches.Visit conferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online refer- ence library for programmers and IT professionals. Conduct searches across more than 1,000 books. Subscribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on yourBookshelf fromcoverto coverorsim- ply flip to the page you need. Try it today for free. Dojo The Definitive Guide Matthew A. Russell Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo Dojo: The Definitive Guide by Matthew A. Russell Copyright © 2008 Matthew A. Russell. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. Editor: Simon St.Laurent Indexer: Lucie Haskins Production Editor: Sumita Mukherji Cover Designer: Karen Montgomery Copyeditor: Colleen Gorman Interior Designer: David Futato Proofreader: Sumita Mukherji Illustrator: Robert Romano Printing History: June 2008: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Dojo: The Definitive Guide, the image of a lion-tailed monkey, and related trade dress are trademarks of O’Reilly Media, Inc. Java™ is a trademark of Sun Microsystems, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. This book uses RepKover™, a durable and flexible lay-flat binding. ISBN: 978-0-596-51648-2 [M] This book is dedicated to every single web developer who has lost sleep because of ridiculous browser idiosyncrasies. Table of Contents Foreword . xiii Preface . xv Part I. Base and Core 1. Toolkit Overview . 3 Overview of Dojo’s Architecture 3 Prepping for Development 7 Terminology 12 Bootstrapping 15 Exploring Dojo with Firebug 21 Summary 31 2. Language and Browser Utilities . 32 Looking Up DOM Nodes 32 Type Checking 33 String Utilities 34 Array Processing 35 Managing Source Code with Modules 40 JavaScript Object Utilities 48 Manipulating Object Context 52 DOM Utilities 55 Browser Utilities 62 Summary 66 vii 3. Event Listeners and Pub/Sub Communication . 67 Event and Keyboard Normalization 67 Event Listeners 70 Publish/Subscribe Communication 76 Summary 79 4. AJAX and Server Communication . 80 Quick Overview of AJAX 80 AJAX Made Easy 82 Deferreds 89 Form and HTTP Utilities 98 Cross-Site Scripting with JSONP 99 Core IO 101 JSON Remote Procedure Calls 110 OpenAjax Hub 112 Summary 113 5. Node Manipulation . 114 Query: One Size Fits All 115 NodeList 121 Creating NodeList Extensions 130 Behavior 131 Summary 135 6. Internationalization (i18n) . 136 Introduction 136 Internationalizing a Module 137 Dates, Numbers, and Currency 140 Summary 143 7. Drag-and-Drop . 144 Dragging 144 Dropping 155 Summary 164 8. Animation and Special Effects . 165 Animation 165 Core fx 176 viii | Table of Contents Animation + Drag-and-Drop = Fun! 185 Colors 186 Summary 194 9. Data Abstraction . 196 Shifting the Data Paradigm 196 Data API Overview 197 The APIs 198 Core Implementations of Data APIs 204 Summary 221 10. Simulated Classes and Inheritance . 222 JavaScript Is Not Java 222 One Problem, Many Solutions 223 Simulating Classes with Dojo 227 Multiply Inheriting with Mixins 237 Summary 241 Part II. Dijit and Util 11. Dijit Overview . 245 Motivation for Dijit 245 Accessibility (a11y) 248 Dijit for Designers 251 The Parser 257 Hands-on Dijit with NumberSpinner 261 Overview of Stock Dijits 266 Dijit API Drive-By 270 Summary 271 12. Dijit Anatomy and Lifecycle . 272 Dijit Anatomy 272 Dijit Lifecycle Methods 275 Your First Dijit: HelloWorld 282 Parent-Child Relationships with _Container and _Contained 293 Rapidly Prototyping Widgets in Markup 293 Summary 295 Table of Contents | ix 13. Form Widgets . 297 Drive-By Form Review 297 Form Dijits 300 TextBox Variations 304 FilteringSelect 323 MultiSelect 324 Textarea Variations 325 Button Variations 325 Slider 333 Form 338 Summary 339 14. Layout Widgets . 340 Layout Dijit Commonalities 340 ContentPane 342 BorderContainer 346 StackContainer 351 TabContainer 353 AccordionContainer 355 Rendering and Visibility Considerations 357 Summary 358 15. Application Widgets . 359 Tooltip 359 Dialog Widgets 360 ProgressBar 364 ColorPalette 366 Toolbar 367 Menu 369 TitlePane 374 InlineEditBox 375 Tree 377 Editor 388 Summary 395 16. Build Tools, Testing, and Production Considerations . 396 Building 396 Dojo Objective Harness (DOH) 407 Browser-Based Test Harness 411 x | Table of Contents Performance Considerations 413 Summary 415 A. A Firebug Primer . 417 B. A Brief Survey of DojoX . 428 Index . 431 Table of Contents | xi Foreword 1 Truth be told, it was DHTML that got me kicked out of college. I still vividly recall the 3 A.M. moments when endless trolling of MSDN documenta- tion and W3C specifications and hundreds of comp.lang.javascript posts all coa- lesced into dozens of “what if...” moments. Like hot brands on the hide of my brain, each of these tiny discoveries would not let their mark off of me until I had exhausted all inroads into making the browser do what I wanted it to. Back then, a small community of folks were all doing the same, feverishly one-upping each other and posting to the DHTMLCentral forums with each new component, technique, or hack to make things work on Netscape. Nothing about 7 A.M. Latin conjugations or endless lectures on Java™ held much appeal by comparison to discovering the true beauty of closures, or finally, completely understanding prototypal inheritance. Even my Christmas holidays were engulfed in JavaScript learning and hacking. I’m sure my girlfriend and my parents worried for me greatly, but they never said anything. From the ashes of my truncated academic career came an understanding of open source (http://opensource.org), lasting friendships, and, eventually, Dojo. Overtime, the job of the DHTML hackerhas changed. We know most of the tricks that we can expect a browser to do, and where there is overlap between browsers, we’ve probably already exploited it...just look at the depth and diversity of modules in Dijit and DojoX. The work of a DHTML/Ajax developer now is to press the avail- able technology into the service of users and developers in ways that are better for end users and developers alike. The story of Dojo is the story of that transition. A beautiful architecture that fails to deliver better things in the lives of users is a fail- ure. Likewise, beautiful graphics and interfaces that aren’t maintainable, can’t be coherently understood by developers, and that make designer/developer collabora- tion harder aren’t going to get us where we want to be. All of us involved with Dojo have matured along with the Web, and with the release of Dojo 1.0 and this book, it’s safe to say that Dojo has fully arrived. The roadmap documents we started so long ago now have all of the boxes checked, sites that serve billions of page views a month lean on Dojo for their entire user experience, and large teams of designers and developers work together to create better experiences on top of the toolkit. xiii These kinds of accomplishments aren’t the work of one person, or even a small team. The numberof people who have contributedto Dojo’s evolution, believed in the project, and worked together to deliver a better Web are too numerous to mention. We copied what we thought were the best bits of the structures of other projects, and the result has been a level playing field and rules that are fair to users, contributors, and sponsors alike. Dojo is proof that open source isn’t just a handy distribution model for closed systems, but that collaborative, open projects can thrive when they adopt policies that let users trust a project and when those inside the project finds ways to trust each other. For all of the technical achievements embodied in the tool- kit, I’m most proud that we’ve done it in the open, with anyone who will join us, and done it honestly. We set out to build a project that values all kinds of contributions, not just code. A project that would help change the tone of open source develop- ment to encourage collegial, civil discourse. A project dedicated to building with the community and not to treat users as “them.” “They” are “us” and this book makes plain the open philosophy underwhich the toolkit was built, and by which we encourage all those reading it to help us evolve it for the future.