Tuesday, October 7, 2014

Book Review: AngularJS Essentials

The subtitle of Rodrigo Branas's AngularJS Essentials (Packt Publishing) is, "Design and construct reusable, maintainable, and modular web applications with AngularJS." This post is a review of the electronic (PDF) format of this book on AngularJS.

AngularJS Essentials consists of eight chapters and roughly 150 pages of substantive content. The Preface of AngularJS Essentials briefly describes the content of each of the book's eight chapters and recommends that readers have access to an IDE or text editor and a web browser to implement the examples included in the book.

Chapter 1: Getting Started with AngularJS

Chapter 1 of AngularJS Essentials devotes its first page to a history of HTML and web development that had led to frameworks such as AngularJS. Branas then describes AngularJS as "an open source, client-side JavaScript framework that promotes a high-productivity web development experience." I think Branas articulates well what AngularJS brings to web development when he writes that AngularJS is based on the idea that "declarative programming is the best choice to construct the user interface" and "imperative programming is much better and preferred to implement an application's business logic." AngularJS achieves this, Branas states, by extending HTML.

Chapter 1's introduction to AngularJS's architecture describes it as supporting applications implementing a model-view-whatever (MVW) pattern. The chapter briefly talks about the view, model, and controller/services/filters aspect of an AngularJS application. This chapter is also the first place I have seen POJO used as an identifying acronym for Plain Old JavaScript Object rather than for the more well-known use as Plain Old Java Object.

The first chapter of AngularJS Essentials introduces a parking lot application example that is used throughout the book. As part of this initial code listing, the author describes how to download regular or minified versions of AngularJS from the AngularJS site or from the Google Content Delivery Network (CDN).

AngularJS Essential's initial chapter concludes with a section briefly describing four potential styles of code organization ("most used and discussed styles in the JavaScript community") for an AngularJS application.

Chapter 2: Creating Reusable Components with Directives

The second chapter of AngularJS Essentials begins with a single page background discussion on the Document Object Model (DOM) and explains how AngularJS looks for "attributes known as directives" in the constructed DOM tree. The author then describes a directive as an "extension of the HTML vocabulary that allows us to create new behaviors."

After briefly describing which constructs in the DOM a directive can be applied to, Chapter 2 moves onto coverage of AngularJS built-in directives (ngApp, ngController, ngBind, ngBindHtml, ngRepeat, ngModel, ngClick, ngDisable, ngClass, ngOptions, ngStyle, ngShow/ngHide, ngIf, ngInclude) with one to three paragraphs and (in most cases) a code listing covering each built-in directive.

After a brief section on reorganizing the book's sample application, AngularJS Essentials covers custom directive creation. This coverage begins with a demonstration of creation of a Directive Definition Object and discussion of template, templateURL, replace, restrict, scope, transclude, link, require, and controller. The chapter references, but does not describe in any detail, the $compile service.

AngularJS Animation is the concluding topic of Chapter 2.

Chapter 3: Data Handling

The third chapter of AngularJS Essentials introduces "data handling using AngularJS" and specifically focuses on expressions, filters, and form validation. The author describes an AngularJS expression as "a simple piece of code that will be evaluated by the framework and can be written between double curly brackets."

An AngularJS filter is described as "the perfect solution to easily perform any data manipulation." The chapter briefly discusses several provided AngularJS filters (currency, date, number, filter, json, limitTo, lowercase, uppercase, and orderBy) used in conjunction with expressions and also briefly describes using filters generally with a controller. Chapter 3 also provides an overview of and simple examples of custom filter creation.

The section of Chapter 3 on form validation describes and illustrates use of several built-in directives commonly used with form validation and the input element (ng-required, ng-minlength, and ng-pattern). This portion of the third chapter also describes and illustrates used of objects $pristine, $dirty, and $error.

Chapter 4: Dependency Injection and Services

Chapter 4 of AngularJS Essentials begins with a discussion of AngularJS's support of dependency injection and provides an example and associated discussion to illustrate why array notation is often the preferred approach for AngularJS applications that are to be minified.

Chapter 4's coverage of "creating services" begins by defining a service as "a singleton object that has its life cycle controlled by the framework" that "can be used by any other component such as controllers, directives, filters, and even other services." This section examines creating of an AngularJS service from a factory and includes discussion on the Revealing Module Pattern and the Immediately-Invoked Function Expression pattern.

Chapter 4 concludes with coverage of AngularJS built-in services. The chapter includes this sentence that made me chuckle (I have added the emphasis in italics): "Introduced a long time ago by Roy Fielding, the REST method, or Representational State Transfer, has become one of the most adopted architecture styles in the last few years." Fielding's thesis on REST is dated 2000, which by some people's perspective certainly qualifies as "a long time ago." In addition to very brief coverage of REST, the chapter briefly describes HTTP (especially status codes), JSON, and Ajax. The chapter also illustrates applying an HTTP facade, HTTP headers, caching, and interceptors in an AngularJS application.

Chapter 4 devotes a section to single page application development with AngularJS's $route service and $location service. The chapter also introduces logging with the $log service and scheduling times for specific behaviors to occur with $timeout and $interval services. The "Asynchronous with a promise-deferred pattern" section of Chapter 4 describes use of AngularJS's deferred API and promise API.

Chapter 5: Scope

AngularJS Essentials's fifth chapter open by describing Scope as "an object that acts as a shared context between the view and the controller that allows these layers to exchange information related to the application model." Similarly, the AngularJS Developer Guide describes Scope as "the glue between application controller and the view." The author of AngularJS Essentials adds, "Both sides are kept synchronized along the way through a mechanism called two-way data binding."

The section of Chapter 5 on two-way data binding illustrates use of $apply and $watch. Another section of Chapter 5 covers best practices related to use of AngularJS scope. There is also very brief coverage of $rootScope.

The "Scope Broadcasting" section of Chapter 5 looks at use of the $broadcast and on functions.

Chapter 6: Modules

The focus of AngularJS Essentials's sixth chapter is on AngularJS modules. This relatively short chapter demonstrates how to create modules by creating three modules for the book's sample application. The chapter concludes with a references to some of the most popular publicly available AngularJS modules.

Chapter 7: Unit Testing

Chapter 7 of AngularJS Essentials focuses on unit testing AngularJS applications. As part of this coverage, it describes use of the ngMock, Jasmine framework, and Karma test runner. The chapter illustrates testing AngularJS Services, Controllers, Filters, and Directives. It also demonstrates mocking with AngularJS. In particular, use of $httpBackend to mock a backend for testing components that depend on the $http service is illustrated. I appreciated AngularJS Essentials dedicating a chapter to unit testing and mocking with AngularJS.

Chapter 8: Automating the Workflow

Grunt and Bower are the focus of the eighth and final chapter of AngularJS Essentials. This chapter provides step-by-step instructions with examples for creating a distribution package and executing the workflow with Grunt. It also demonstrates managing packages with Bower.

General Observations
  • As its title implies, AngularJS Essentials focuses on the more important aspects of AngularJS. Although some web development history and general web development techniques are very briefly mentioned, they are generally minimalist in nature and allow the focus to be directly on AngularJS. AngularJS Essentials is much shorter than other books on the subject (one is nearly 700 pages long), but also does not spend numerous pages covering CSS, HTML, JavaScript and other technologies that are well covered in other books and are often already familiar to developers learning AngularJS.
  • Code listings in AngularJS Essentials are black font on white background even in the electronic copy I reviewed and do not have line numbers. Several of the code listings use bold emphasis to highlight the specific concept being illustrated with the code listing.
  • AngularJS Essentials includes a couple graphics depicting described topics. These are attributed to the AngularJS documentation as the source of the graphics.
  • With some frameworks, libraries, or languages, a good book is important because of a lack of available resources on the subject. That is not the case with the wildly popular AngularJS that not only includes good standard documentation, but enjoys a large community of users generating useful resources. These numerous good resources covering AngularJS online include AngularJS documentation such as Official AngularJS Tutorial, AngularJS Developer Guide, and AngularJS API Reference, as well as third-party resources such as ng-newsletter articles, Thinkster/Egghead, StackOverflow, and Dan Wahlin's AngularJS posts.
  • Book reviews are by their nature opinionated and different books sometimes appeal to different people. Additional reviews of AngularJS Essentials can be found on Amazon.com and GoodReads.com. I think the composite impression one gets from reading the reviews as currently available on these sites provides a fair overall representation of AngularJS Essentials and its strengths and weaknesses.

AngularJS Essentials introduces the essentials of AngularJS and spends minimal time covering preliminary and historical details. As "essentials" implies, it is not intended to be a thorough reference. For those comfortable with learning from online tutorials and web sites, AngularJS Essentials is probably unnecessary in the well-documented world of AngularJS. However, I can recommend AngularJS Essentials for those looking for a good complement to the numerous useful online resources on learning AngularJS. AngularJS Essentials can provide a single source of quick structure for learning about AngularJS and how AngularJS can be used for web development. AngularJS Essentials is best suited for readers who already have basic familiarity with web development technologies such as HTML, JavaScript, and CSS, and want a book focused almost entirely on AngularJS and tools and techniques that can be used with AngularJS.

No comments: