Create reusable UI components and widgets using Backbone.js
30 Mar 2012I was building an application in Backbone.js recently and I wanted to experiment with an idea I had to build UI components using Backbone.js. I was given the opportunity when we required a notification element to be displayed on the page. It is possible, to do this in CSS or say using Twitter Bootstrap, however, I wanted to turn this into a basic Backbone.js UI component.
In order to do this we need to think about how the component can be reused in different applications; i.e. not tied directly to any domain specific markup, schema or business logic in the application that is being built. Now, you would probably want to create a separate repository for this project since, after all, it is not strictly meant to live inside an application (unless, of course, your not interested to use the component in any other project other than the one your working on).
The Template
The template has a single placeholder message which can be set in the instance of the model that is used in the application. Note, from the line class=”alert fade in” that this particular component will work best with Twitter Bootstrap.
The Model
The Notification model has two defaults set: message and status (which is another model). Notice the namespace com.tweetegy.ui used to keep the objects separate from the application.
Notification Status Types
The Notification Status Types Model acts as an enum holding strings to represent the status of each notification that is shown. This is used to control the style of the notification.
Backbone View
This is the object that wraps everything up and allows us to reuse this as a component (as we will see later). This is just a standard Backbone View, with an added helper method updateStyleFromStatus which changes the style depending on the status of the Notification and closeNotifcation that hides it.
The other important thing to note is the line this.model.bind(“change”, this.render); which is where we use the ‘magic’ of Backbone data binding to render our notification anytime the message or status of the applications notification model changes. It means that all we need to do in our application is set either the message or the status and the view will render itself.
Using the component
I used it in an application called “Front Desk” and initialized it in the bootstrap.js file during application initialziation:
As mentioned above, in the Backbone View we bind to the models change event to render the view which means all we need to do is change the data in our applications NotificationModel instance (in this case com.tweetegy.FrontDesk.notification) and the Notification will automatically be rendered. Here is an example:
Conclusion
I have always liked the idea of UI components and using Backbone templates, views and models really makes it easy to build a highly customized UI library for your application, department, company or even to share with the entire community!
I have written another component which uses a Backbone Collection and can be used in a form to collect data from the user. This is a little more complex than the notification example here and I will cover it in a future blog post! Meanwhile, happy coding!