Web UI Architecture
Though some of the concepts in this course are still relevant, overall this course does not reflect our current course standards or industry best practices.
Table of Contents
Architecture: Decisions you make about your applicationArchitecture is the decisions you are making about your application. Architecture is not externalizing files or an organized file system. Good architecture uses minimal boilerplate code and a carefully curated library of tools. It’s a balance of performance and maintenance. Architecture should be able to wire up a new static page given mostly DOM references and server endpoints. Scaffolding is your architecture.
Example: Shopping CartA shopping cart is something most of us have encountered. We’ll use a shopping cart as our project while exploring proper architecture. The first step is to define our objects, states, interactions, and what we know about the application.
Write (some) implementation firstPro-tip: Write some of your implementation first. Garann walks us through a naïve outline of our shopping cart application with some basic implementation. Start thinking about inheritance. Reveal the abstractions you will need. Looking through the implementation outline, Garann asks the audience to suggest areas that require some abstraction.
Part 2: Abstractions and Frameworks
Messaging, Rendering, and Server InteractionsMessaging is responsible for notifying subscribers, caching, and tracking the context of events. Decoupling is encouraged by the ability of messaging to pass data along with the event. Rendering code provides rendered HTML by interpolating data while caching compiled templates. Server interactions pass server data to requesting objects and vice versa. They are also allow the decoupling of URLs from instance code and have a built-in notification system for when data is sent/received.
State Management, Validation, and Framework ContextState management allows objects to have one or more states. As a state changes, the correct chain of events will be fired of and appropriate properties will be modified. - Question: Do you use routes with state management? Validation engines provide the user with results and update stating information based on valid/invalid operations. Validation function should be abstracted whenever possible to promote reusability. The framework context contains all the framework abstractions and distributes functionality to any registered objects. All framework code and implementations are initialized in the context and messaging/state management are enabled above the application level.
Implementing a FrameworkImplementing a framework starts with sketching out a small set of functionality and designing as much of the implementation as possible. Downloading your abstractions (as opposed to writing them yourself) can be beneficial. Third-party frameworks are almost always the right answer. Choose your frameworks carefully, though. How large is the base library? How much scaffolding is required by the implementation? Will it play nice with other libraries or will it have to be shoehorned into your application pattern.
Angular JSAngular JS looks a lot like a template engine and contains a wide library of services and objects. There are a number of built-in DOM elements. Angular JS code demonstration. - Question: How does the Angular framework wire up the DOM? The presentation in Angular JS is largely in HTML with simple event handling available. Angular also provides a straight-forward routing API. Data focuses heavily on bindings and, in general, Angular encourages the use of MVC for architecture.
BackboneBackbone is an MV* framework focused mostly on logic and very little on the DOM. It relies on Underscore for many utilities. Models in Backbone are built for inheritance and can easily be organized into collections. Views contain all the information about where the view is rendered, but the rendering has to be supplied by the developer.
EmberEmber is a hybrid of Angular and Backbone. Models, views and controllers are called explicitly and theirs a limited set of DOM interactions. Bindings in Ember are bi-directional allowing all modules to be in sync without additional effort. Computed properties give developers flexibility to use the same get/set pattern with compositional results. The auto-updating templates do not require explicit rendering. Selecting a framework for our shopping cart example depends on how trivial the abstractions might be, whether implementation will be mostly templating, and where the data will need to live.
Questions & CodeGarann fields a couple questions from the first session and discusses some resources suggested by audience members. Starting files for coding the shopping cart example. Garann continues to discuss coding the shopping cart and her preferences around frameworks and templates.
Part 3: Outside Tools
The Reason for Third-Party ToolsThird-party tools implore best practices, save time, and make it easy for code and teams to scale. They may involve managing CSS and/or markup or simply be a set of utilities, plugins or widgets.
CSS ToolsCSS is a substantial part of your application and should be decoupled. The right tool can eliminate classes of layout and presentation problems. Object Oriented CSS: Normalize CSS - https://necolas.github.io/normalize.css/
Markup ToolsLike CSS, markup is equally as substantial in an application. It’s the foundation behind views and templates. Different deployments allow for the use of non-HTML syntax when defining markup. Markdown - http://daringfireball.net/projects/markdown/ HAML - http://haml.info/
CSS and Markup ToolsThere are also tools that work with both HTML and CSS. They tend to be more unified and highly configurable. Twitter Bootstrap - http://twitter.github.io/bootstrap/ HTML5 Boilerplate - http://html5boilerplate.com/
Dependency Management ToolsDependency management reduces the need to manually load/import scripts. It removes unnecessary code from the global namespace and is a necessity for robust architectures. Require JS: Yepnope JS - http://yepnopejs.com/
UtilitiesUtility tools give you big or small abstractions. They may or may not involve DOM manipulation and allow instant access to best practices. Finding the balance between covering use cases and code size is the key to a good utility tool. Underscore - http://underscorejs.org/ Modernizr - http://modernizr.com/ jQuery - http://jquery.com
Plugins and WidgetsIf you can imagine it, there’s a plugin for it. Don’t reinvent the wheel. While plugins are great, they tend to confine you to another tool. Widgets can be more flexible and easier to incorporate into any pattern. Finding the right plugin/widget can often times be difficult due to a lack of support or unusual implementation.
Part 4: Outside Tools, continued
Testing ToolsTesting is easier said than done. It can be simple and straight forward with synchronous code. Asynchronous code can be much more complex. QUnit - http://qunitjs.com/ Mocha - http://visionmedia.github.io/mocha/ Audience Suggested:
LintingLinters check code for possible errors and enforce a minimum set of optimizations. They can also help prevent surprises due to coercion. JSLint - http://www.jslint.com/ JSHint - http://www.jshint.com/
DocumentationDocumentation will decay if not given proper time and diligence. Postponing documentation causes it to become an insurmountable task. While inline comments are better than nothing, it’s best to adopt a tool to make documentation easier. JSDoc - https://github.com/jsdoc3/jsdoc
Deployment ProcessAs an absolute minimum, concatenation and minification should be a part of a deployment process. Node has allowed for many open-source tools to emerge giving developers many options. UglifyJS - https://github.com/mishoo/UglifyJS Grunt - http://gruntjs.com/
All-In-One ToolsMature architectures typically follow the same steps throughout the development process. In these cases, they tend to use the same tools. Finding an all-in-one tool can eliminate a lot of repeated work. Enterprise tools can fit this description but may be less configurable. Garann, of course, favors the open sources. Yeoman - http://yeoman.io/