Over a million developers have joined DZone.

Applying MVC to Your Frontend Application Using Require and Backbone

DZone's Guide to

Applying MVC to Your Frontend Application Using Require and Backbone

· Web Dev Zone ·
Free Resource

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

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

Take a look at an Indigo.Design sample application to learn more about how apps are created with design to code software.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}