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:

/project
 /css
  bootstrap.css
 /scripts
  /app
      /Collections
        NavbarCollection.js
      /Router
        ApplicationRouter.js
      /Templates
        header.js
        navbar.js
      /Views
        HeaderView.js
        HomePageView.js
        NavbarView.js
  /lib
        backbone.js
        bootstrap-dropdown.js
        jquery.js
        require.js
        text.js
        underscore.js

  main.js
index.html


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: https://github.com/sagar-ganatra/frontendMVC

Comments

Popular posts from this blog

File upload and Progress events with HTML5 XmlHttpRequest Level 2

The XmlHttpRequest Level 2 specification adds several enhancements to the XmlHttpRequest object. Last week I had blogged about cross-origin-requests and how it is different from Flash\Silverlight's approach .  With Level 2 specification one can upload the file to the server by passing the file object to the send method. In this post I'll try to explore uploading file using XmlHttpRequest 2 in conjunction with the progress events. I'll also provide a description on the new HTML5 tag -  progress which can be updated while the file is being uploaded to the server. And of course, some ColdFusion code that will show how the file is accepted and stored on the server directory.

Server sent events with HTML5 and ColdFusion

There are several ways to interact with the server apart from the traditional request\response and refresh all protocol. They are polling, long polling, Ajax and Websockets ( pusherapp ). Of all these Ajax and Websockets have been very popular. There is another way to interact with the server such that the server can send notifications to the client using Server Sent Events (SSE) . SSE is a part of HTML5 spec:  http://dev.w3.org/html5/eventsource/

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.