I, like most frontend developers, love jQuery (I’m traditionally a backend developer BTW). It can do some amazing things with the DOM. It is very easy to work with, and works with native JavaScript like a charm. Basically, jQuery adds a single function, $, and overloads it with a ton of possibilities. Seeing as it provides just a single function, it makes zero assumptions about the structure of your project, and integrates easily into any JavaScript project.
Unfortunately, that last feature is often the bane of any mid to large sized project using jQuery. Before long you inevitably end up with spaghetti code.
That’s where something like Backbone.js comes in. Backbone provides an MVC like framework in your frontend. If you are anything like me, this is (was) an instant turnoff. Why in the heck would I want to overcomplicate my frontend with MVC code? Isn’t that what the backend is for? I’m already working with data in the backend, generating views, and passing them to the frontend, all I need is some interface interactions!
Backbone allows you to build a Single Page Application (SPA), and handles routing, controllers, models (which also exist in the backend), and views. Routing is handled by reading the URL hash tag and applying a RegEx you specify and routes the user to the applicable code. Controllers are basically built into the routers, or the views, depending on how you look at things. Models are a local structure which mimics your backend models, although usually not as complex. View
Tags: Backbone.js, Event Based Programming, jQuery, MVC
Category: Best Practices Articles • Javascript and jQuery Tutorials




