Recently, I visited the scientific software and data management (SSDM) group at ETH to give a talk about web components and dart frameworks. Here is a summary.
Looking back: a brief History of Web Apps
We live in interesting times: there are many frameworks, languages out there, and in order to understand the context and why things are how they are, it helps to look at the history. The web as a platform for applications has evolved in an erratic process, which I will divide into three broad phases:- the "Middle ages": content generated on the server-side, Java born with the promise of applets, Javascript launched as a tool for web designers (read: non-programmers), browser wars [1]. Client-side coding was a matter of achieving visual effects, client-server communication happened through forms and page-loads.
- the "Renaissance": asynchronous Javascript[2] becomes ubiquitous, client-server communication is now decoupled from forms and page-loads. Rich client-side apps like GMail or Google Maps demonstrate its use.
- "Modern History": high-performance layout and javascript engines are the norm. HTML5 delivers a reinterpretation of browser documents and APIs, tailored to web applications development. Mobile devices get high resolution screens and useful browsers.
HTML5 continues to evolve, and not all HTML5 technologies are not supported on every browser (so developers check on caniuse.com). The idea of a Polyfill is born: adapter code that simulates functionality that does not exist natively in the browser.
Web Components
With application development on the web becoming the norm, the traditional questions of software engineering arise: how to approach development of large systems? how to encapsulate and reuse? Web components [3,4] offer an answer.
Web Components are four technologies that together achieve reusable units of behavior:
- custom elements
- templates
- shadow DOM
- HTML imports
These technologies are being standardized and implemented in a few browser. Even when not available natively, there is the possibility of using polyfills.
As with any web technology, there are good documentation resources available on the web that explain these. In the following, I'll just include a simple example for completeness.
Defining a new custom element
Say we want to write a bug-tracking system that displays a number of bug entries. We'd like to treat the entries as we would treat other HTML elements (note the fancy new HTML5 syntax):
<bug-entry bug-id=1e4f bug-status=open>
This is possible with custom elements, we'd want to register the element like so:
// Create a new object based of the HTMLElement prototype. var BugEntryProto = Object.create(HTMLElement.prototype); // Set up the element. BugEntryProto.createdCallback = function() { ...render bug entry, e.g. with img, click handler/link } // Register the new element. var BugEntry = document.registerElement('bug-entry', { prototype: BugEntryProto });
Filling the element with content
Most of the time, custom elements will actually correspond to some other markup. With HTML templates, it is possible to include template markup in documents and instantiate it.<template id=”bug-entry-template”> <tr><td><img class=”bug-image”></td> <td><div class=”bug-title”></td></tr> </template> BugEntryProto.createdCallback = function() { … var template = document.querySelector('#bug-entry-template'); var clone = document.importNode(template.content, true); … }
Specifying layout and style of the content
In HTML, layout and style is specified using CSS, however the specification is global. In order to have a self-contained, encapsulated unit of code, we need some mechanism that enables reuse without worrying about style. This is what is achieved with the shadow DOM: content can be added to the DOM in a way that keeps it separate from the rest of the DOM.BugEntryProto.createdCallback = function() { // create shadow DOM var root = this.createShadowRoot(); // Adding a template var template = document.querySelector('#bug-entry-template'); var clone = document.importNode(template.content, true); … root.appendChild(clone); }
Importing the new component
The sections above have defined a component. Now we would like to be able to use the component. HTML imports provide a way to import components easily:<link rel="import" href="bug-entry.html">
Dart Frameworks
Polymer
The Polymer.js library provides many things:- Polyfills to get web components in browsers that don't support them
- Many ready-to-use components that follow Material Design UX specifications
- Tools like vulcanize that translate away HTML imports for the purposes of deployment
The polymer.dart library is a wrapper around polymer.js that makes the polyfill as well as the components available in dart. It also offers an easy, integrated syntax which is mainly possible because of reflection. Instead of using vulcanize, users of polymer.dart rely on the polymer transformer which translates away the use of reflection and also takes care of combining all the imported components into one unit of deployment.
There is a bottom-up development approach suggested by the availability of polymer: start building basic components and assemble them into every larger ones until you have an application. It is important to note that polymer is not an application development framework: things like logical pages, views, routing are not standardized, but can be provided by components.
Angular, Angular.dart and Angular2
Angular existed before web components and polymer. It was always a framework for application development, so provides facilities for pages, views, routing. The dart version of angular is significantly different from the js version, again because the use of reflection permits a nice style that uses annotations. Angular does not work seamlessly with web components defined in polymer, although it is possible to make the two things work with a bit of glue code.The newer version, angular2, is backwards incompatible with angular (though concepts are similar). It aims to provide seamless integration of web components (as defined by the standard). It is noteworthy that annotations are advocated as the primary means to set up angular2 components.
Conclusion
Web development has become even more interesting with the promise of reusable, encapsulated components. Work on these is still in progress, but I for one am looking forward to more structured web applications that can draw from a large set of reusable components.References
[1] Brendan Eich interview in InfoWorld, 2008[2] https://en.wikipedia.org/wiki/Ajax_(programming)
[3] Web Components description in Mozilla Developer Network
[4] https://en.wikipedia.org/wiki/Web_Components
[5] Are we componentized yet?