JavaScript development has come a long way in recent years, emerging from a miasma of inline scripts and a chain of callbacks to embrace application-level programming, through a variety of frameworks that have managed to corral its unruly nature, imposing structure where seemingly none existed.
However, these frameworks often seem to subtract from the experience of JavaScript, providing heavy APIs and requiring extensive boilerplate code.
Flight, with the same goal in mind, takes a different path. It builds on a structure that forms the heart of any web page or application: the DOM. Flight provides atomic components that are joined together as an infinite lattice, adding functionality without increasing complexity, and allowing for truly scalable applications.
In this book, I aim to provide a working knowledge of Flight to both seasoned application developers and novices coming from a traditional JavaScript development background.
I was able to write this book from a unique perspective, having been given the opportunity to work with Flight on a major JavaScript application (TweetDeck) before its initial open source release, and as a part of the Twitter organization, with direct access to those responsible for designing Flight, the Twitter Web Core team.
Their ongoing support, advice, and understanding of the intentions behind Flight has hopefully led this book to be more than just a technical manual, allowing me to dig deep into how to design applications that conform to Flight's ideals.
Thanks go to Dan Webb, Angus Kroll, Todd Kloots, and Kenneth Kufulk for their help and advice, and also to Sol Plant who was my partner in introducing Flight to TweetDeck; to Nicolas Gallagher for his work on the Yo Flight Generator; to Cameron Hunter, Katsuya Noguchi, Simon Smith, and Andrey Popp for helping to review the book; and to Emma Dingle for being there when the going got tough.
Chapter 1, What is Flight?, covers the basics of how Flight works and the problems it aims to solve.
Chapter 2, The Advantages of Flight, details Flight's advantages over other frameworks. This includes its shallow-learning curve, reliability, reusability, agnostic architecture, performance, and the idea of well-organized freedom.
Chapter 3, Flight in the Wild, captures Flight's use in the real world before and after its release, by providing examples of applications and open source projects using Flight.
Chapter 4, Building a Flight Application, explains how to scaffold a Flight application with the Yeoman Flight Generator and walks through the resulting application structure.
Chapter 5, Components, aims to provide an overview of what components are and how to build them.
Chapter 6, UI Components, provides examples on how to listen for browser events, how to access elements within a component, and the use of defaults and settings within components.
Chapter 7, Data Components, deals with how data components differ from UI components, how to create them, attach them to the DOM, and how to handle UI events and trigger data events.
Chapter 8, Event Naming, discusses the importance of good event names, what an event really is, and also provides an example naming convention.
Chapter 9, Mixins, focusses on what mixins are, when to use them, and how to create them. It also covers Advice, a mechanism used to override or extend existing methods.
Chapter 10, Templating and Event Delegation, discusses various templating approaches in Flight, providing examples of DOM node templating, client-side templating with Hogan, and server-side templating with Grunt. Also covered is how to use generated HTML to determine state and event-delegation for dynamic HTML.
Chapter 11, Web Application Performance, shows how Flight can be used to render pages efficiently and how to work around latency in Ajax requests.
Chapter 12, Testing, provides an example BDD test written in Jasmine, explains what to focus on while testing components, how to gain references to component instances, and how to extend existing test frameworks to handle components, mixins, and events.
Chapter 13, Complexities of Flight Architecture, deals with the problems with nesting components and how to better define components to avoiding nesting.
Appendix, Flight API Reference, gives an overview of the essential API methods including how to create components and mixins, how to use Advice, listen for and trigger events, and define default attributes.
To follow the examples in this book, you should have a working computer and a text editor. All the software required for installing and running Flight is covered within the book.
This book is for anyone who wants to build Flight components, mixins, and applications. No previous knowledge of Flight or other application frameworks is required. A good understanding of JavaScript is required and any knowledge of jQuery and AMD will help, though is not essential.
In this book, you will find a number of styles of text that distinguishes between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "Generally, data components only trigger a single data event, for example, dataTask
, dataTags
."
A block of code is set as follows:
define(function (require) { // import dependencies var defineComponent = require('flight/lib/component'); // export component constructor return defineComponent(helloWorld); // component definition function helloWorld () {}; });
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
define(function (require) {
var defineComponent = require('flight/lib/component');
var withTextUtils = require('component/with_text_utils');
// mixin other mixins
var withLocalStorage = function() {
compose.mixin(this, [withTextUtils]);
};
});
Any command-line input or output is written as follows:
mkdir flight-example && cd $_ yo flight example
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Press cmd / Ctrl + Alt + J to open Console in Chrome."
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>
, and mention the book title via the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]>
if you are having a problem with any aspect of the book, and we will do our best to address it.