I’m relatively new to the world of single page applications and have just finished a project built using AngularJS. What we built was impressive but I had the following reservations:
- The learning curve was steep (it’s not just me right?!).
- The code was difficult to dive into for new developers.
- It was very opinionated. It was the AngularJS way or the incredibly-painful-bending-over-backwards-to-make-it-work-the-way-you-want way.
It’s not just AngularJS that has a lot of these problems. Frameworks are getting bigger and bigger and more and more opinionated. Urgh. There must be an easy way for a simpleton like myself.
My interest was piqued then when I heard about a new framework called Flight brought to you by Twitter. It bills itself as non-opinionated and a doddle to learn.
In this blog I’ll introduce the framework in a little more depth by building and testing a simple application. Along the way we’ll meet some of the key features and by the end you should have a good grasp of the fundamentals.
If you want to skip the waffle and go straight to the code then click here.
Assumptions
This blog assumes the reader has a basic knowledge of javascript, jQuery and Jasmine and a healthy curiosity. The application we will be building is plumbed together using RequireJS.
The project
In this blog we’ll build a simple application that has a list of items to select from in the left hand panel. In the right hand panel will be a more detailed view of the selected item. Noddy stuff, but useful to illustrate the basic concepts.
Component parts
Components are the building blocks of a Flight application. A component is a small, cohesive unit of functionality which is attached to a DOM element. They have zero knowledge of each other and once they are attached to the DOM they are on their own. This makes testing them incredibly simple. No dependencies to stub as there are no dependencies! “So how do they communicate with each other?!” you might ask. We’ll get there.
Components are declared using the component method and are attached to the DOM using the attachTo method. For this project we’ll have a listComponent which describes the list panel, a detailsComponent which describes the details panel and a dataComponent which will hold our data (we’ll plonk this on the document node).
Here’s our dataComponent:
Our listComponent:
Our detailsComponent:
As it stands this won’t do anything as none of our components are attached to the DOM so let’s do that now. I used a little bootstrap for the styling. Here’s our html:
Here’s our requireMain.js
which we will use to start up require:
And here is our main.js
which we use to start the application:
Bosh. Three components attached to the DOM. Now what…
The main event
As mentioned before these components know nothing about each other and this is intentional. Components communicate to each other by listening for and triggering events. As applications get larger, care must be taken when naming events. A nice naming convention is described here.
Right then, with that swirling round our heads, let’s put some event handling and triggering in our application.
Take a second to look at the events being triggered and handled and you should be able to follow the flow of the application. You should be able to follow the “event conversations” between the three components.
Here’s what happens when the application loads:
- The listComponent requests the data on startup by triggering the ‘uiItemsRequested’ event.
- The dataComponent hears this and serves up the data using the ‘dataItemsServed’ event with the data attached.
- The listComponent hears this and renders the list. Sweet!
And here’s what happens when a user clicks an item in the list:
- The listComponent fires off an ‘uiItemSelectionChanged’ event with the id of the selected item as the event payload.
- The detailsComponent hears this and sends out the ‘uiItemRequested’ event.
- The dataComponent hears this and triggers the ‘dataItemServed’ event.
- The detailsComponent is listening for this and renders the details. Hurrah!
“Hang on a second, what’s that this.after('initialize', function() {...});
bit?!” you might ask. Very astute of you. It is an example of advice and allows us to modify existing code by adding extra functionality. In our case this can be read as “after initialisation, add our event handlers and, in the case of the listComponent, trigger the ‘uiItemsRequested’ event”. The this.after('initialize', function() {...});
pattern is the most common you will use and is used to setup the component.
Mixin’ it up
Pretty neat, we have a working application. It won’t win awards… yet. But we’re about to add some logging to the console. Surely that will swing the judges. We don’t want to repeat our code again and again so what’s the answer? Mixins. Mixins are bits of code that we can make available to multiple components. Lets make our loggerMixin with a single info
function:
And now let’s add this bad boy to a component:
It’s as easy as including the mixin when defining the component. We now have access to the this.info
function that we defined in the mixin. It’s easy to think of more complex examples where this would be very useful.
Testing times
Because components don’t depend on each other and they have a well defined API (i.e. given this event with this data, I expect that event with that data to be triggered) they are very easy to test. There are helpful libraries which extend jasmine (jasmine-flight) and mocha (mocha-flight), depending on your preference, which have additional functions for initialising components and spying on events. Here is a simple test for the dataComponent written using the jasmine-flight library:
The this.setupComponent
function must be called before you start testing and can be called with test html and options (although I haven’t needed them for this test). Very simple I’m sure you’ll agree.
Conclusion
Flight isn’t designed to take the place of AngularJS, KnockoutJS or any other MV* framework. Instead it provides a neat way to split up your code by piggy-backing on technology that we all understand (events and jQuery). It forces you to write decoupled and easily testable code. Apart from that you can structure your application as you wish.
In our simple application we didn’t have any particularly heavy lifting to do with any of the data being passed around but it’s easy to see how Flight is non-prescriptive in this sense. You could have a chart component that handles events by drawing a chart with D3. Maybe you want to use Mustache to render the event data as html? Maybe you could have a component which is described by a KnockoutJS viewmodel? Up to you.
Using Flight isn’t all hunky dory though. It’s worth noting that the community support isn’t particularly strong for Flight at the moment. Flawed metric alert: at the time of writing there are 80989 AngularJS stackoverflow questions compared with 18 Flight questions.
Another thing I found was that because it isn’t an end-to-end solution like AngularJS there was a little frustrating setup at the beginning particularly with testing. The solution generators didn’t quite work and the documentation was a little out of date but all-in-all, development was pretty painless.
I hope you would now consider using Flight in your next project. Again, the full source code can be found here.
I am a student, not a teacher so any questions/corrections/observations are more than welcome.