Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.
What are your thoughts on Backbone? There seem to be a lot of arguments for and against its uses – tell us how you feel!
So whereas, if you just wanted to select an item on the page and make it either disappear or appear or change some styles on it, some quick and dirty jQuery might be a great approach if you have a lot of things on your page that have sort of a data structure to them. In the typical canonical example of a to do list, each item on the to do is model. It has certain attributes that you can set and read.
You can group it into a collection, and you can associate it with views which allow you to sort of render it out into HTML, without necessarily having to go to the server and get it to return a little snippet of HTML that represents your to do. All of that is on the client-side, based on JSON data that you get from the server.
JGB: So when exactly would I use Backbone.js, then?
Louis: I’ve touched on this a little. The main use case for something like Backbone is when you find yourself hitting the limits of your ability to organize your code. If you find yourself writing a lot of boilerplate to sort of grab data out of JSON that you’re getting from the server and bootstrap it into HTML, if you’re doing a lot of concatenating strings together to make markup that represents an object.
So every time you want to add a to do list to the markup, if what you’re doing is getting the text of the to do to the person that is entered into a form and then sort of going to the steps of putting an LI element on either side of it, and doing this is garbage. You’re doing it kind of in a very ad hoc fashion. That’s sort of one of the cases where something like Backbone could come in handy, because it lets you just sort of say, this is a template that’s associated with the to do model.
All I have to do is create a new to do model, create a view that represents it, call render on the view, and then all this stuff that set up in the background just is ready to happen. So it sort of provides you with this all-purpose glue that you can use to tie together your data structure that comes in from your JSON to your output view via the sort of intermediate of having a really good programmatic representation of the data in the form of a model.
JGB: What are the benefits of Backbone.js?
JGB: So what can you do with Backbone.js?
JGB: Are there other alternatives to Backbone.js, then?
that I’m familiar with that just are very much more feature-full.
JGB: So what kind of background you need to use Background.js?
If you were to set off on your own probably you have all kinds of spaghetti code willy-nilly all over the place. So, yeah, you need some basics of programming knowledge beyond simple really elementary client side scripting.
Jess is a video content creator for Learnable and Sitepoint. She digs all things social media, Web 2.0, music, film, digital media and innovation. Feel free get in touch!