Skip to main content

Applying MVC to your frontend application using Require and Backbone

I've been looking into BackboneJS for quite sometime now and thought it would interesting to build an application using the MVC pattern. Although Backbone provides various components - Models, Collections, Routers and Views; it doesn't provide a framework that can help you to structure the application using these components. While working on this application, I realized that I can separate these components in such a way that they can be made reusable or rather loosely coupled by defining the components in separate files. However, there was an interesting problem that I came across where the Views had a dependency on the Collections to be loaded first and then render the template data. I had read a little about RequireJS that allowed modules to be loaded asynchronously and also in resolving dependency. Thought I would give it a try and see whether it addresses this problem.

 This is how I've structured my application:



In this directory structure you'll see a good number of JavaScript files but I've tried to ensure that the concerns are separated. Here the scripts directory contains two other directories app and lib. The application related files would fall into the app directory and the lib directory would contain all libraries that the application uses. There are two other files index.html and main.js. The index page would define the basic container (although it is not really required) where other views would be rendered (will talk about this little later) and the main.js file would trigger the application rendering logic.

Coming to applying MVC to the application, I have structured the directory in such a way that it is easy to maintain and build on top of it. Here's a picture of how the application would work:

When a request comes to the application, the Router would be invoked and it will route the request to the corresponding View. The views can contain sub-views as well. Here I'm navigating to the Home page when there are no params in the URL. The Home page has the header, navbar, body, footer and maybe some other components. Each of these components or sub-views are made reusable by defining them in their own JavaScript file. Sometimes the views may render static data (in this case the header), but in most of the cases the views may require the data to be fetched from the server. This is where the Backbone Collections would come in to picture. As explained in my previous post a Collection can send a REST request to the server and construct a collection of models.

Now the views would have dependency on the Collection to be loaded and this is where RequireJS would come very handy. RequireJS allows you to define modules and also specify the modules on which it is dependent on. The module would be defined only after all the dependent modules have been loaded completely. Ben has a great post on Using RequireJS For Asynchronous Script Loading And JavaScript Dependency Management.

The Views can then render it by getting the html from the template and compiling it with the data that has been received from the server. The generated code would then be rendered on the specified container. One thing to note here is that I'm using Underscore's templating engine to render the views. I prefer using underscore since Backbone already has a dependency on it and also the fact that it comes with several utility functions. However, one could use any other templating engine to render the generated content on the page.

I've created a simple application that shows static data as well as data returned from the collection. The page has a static header and a Navbar that is constructed by fetching the data from the collection. You can open the console view and see how RequireJS invokes the dependent modules before rendering the data.

You can refer to the source code here:


Popular posts from this blog

Adding beforeRender and afterRender functions to a Backbone View

I was working on a Backbone application that updated the DOM when a response was received from the server. In a Backbone View, the initialize method would perform some operations and then call the render method to update the view. This worked fine, however there was scenario where in I wanted to perform some tasks before and after rendering the view. This can be considered as firing an event before and after the function had completed its execution. I found a very simple way to do this with Underscore's wrap method.

De-obfuscating javascript code in Chrome Developer Tools

I had blogged about JavaScript debugging with Chrome Developer Tools  some time back, wherein I have explained how these developer tools can help in debugging javascript code. Today Google Chrome 12 was released and my Chrome browser was updated to this version. As with every release, there have been some improvements made on performance, usability etc,. One feature that stood out for me is the ability to De-obfuscate the javascript code. What is Minification? Minification is the process of removing unnecessary characters such as white spaces, comments, new lines from the source code. These otherwise would be added to make the code more readable. Minifying the source code helps in reducing the file size and thereby reducing the time taken to download the file. This is the reason why most of the popular javascript libraries such as jQuery are minified. A minified jQuery file is of 31 KB in size where as an uncompressed one is about 229 KB. Unfortunately, debugging minified javascript f

On GraphQL and building an application using React Apollo

When I visualize building an application, I would think of using React and Redux on the front-end which talks to a set of RESTful services built with Node and Hapi (or Express). However, over a period of time, I've realized that this approach does not scale well when you add new features to the front-end. For example, consider a page that displays user information along with courses that a user has enrolled in. At a later point, you decide to add a section that displays popular book titles that one can view and purchase. If every entity is considered as a microservice then to get data from three different microservices would require three http  requests to be sent by the front-end app. The performance of the app would degrade with the increase in the number of http requests. I read about GraphQL and knew that it is an ideal way of building an app and I need not look forward to anything else. The GraphQL layer can be viewed as a facade which sits on top of your RESTful services o