2

Being some random rails dev, and getting around all those backbone/underscore javascript stuff…

Backbone seems interesting, but in a functional point of view (I mean the way you can get your data back from the db and your page can be updated), I can't see any real good reason to move forward to backbone. I'm saying, compared to the RJS approach rails bring.

With backbone, more things happens on the client side; but can't see any fundamental difference besides this fact

Any backbone-evangelist's feedback appreciated

Ben
  • 5,030
  • 6
  • 53
  • 94

1 Answers1

1

It's definitely going to be a matter of preference, but here's some rambling as to why you might want to go with something like Backbone over something like RJS.

RJS is great for when you just need to return code like $('#post_#{@post.id}').slideUp() or something like that. When you want the backend to be in control of the front-end directly. This does make sense for a lot of apps, for sure. But when you start getting into more complex stuff: change the post title, change the post body, change the post tags, change the post title in the 'recent posts' sidebar, etc. This can get difficult to maintain quick. If you change the markup subtly you can break all of your RJS. Also, using RJS, it's pretty difficult to change the whole page and keep the user sane (eg, you will break back buttons and generally stomp all over the state in the browser).

So, for bigger/more end-to-end applications, you can institute something like Backbone as a way to keep your code organized, and move the logic to handle the display of your data to the client. Some benefits:

  • You will put less load on your server, since the client will do a lot of rendering work.
  • You change some markup in a client side template, it's natural and simple to modify the corresponding Backbone.View if there are changes to make there, instead of at the bottom of potentially several controller actions in Rails.
  • You can easily handle navigation between multiple pages/states, and keep that logic on the client.
  • It's very easy and low-barrier to organize your javascript strictly into whatever makes sense for your app (eg, collections.js, models.js, views.js, or maybe app.js for smaller stuff, or maybe post_view.js, posts_collection.js... whatever you need)... again rather than having javascript spread into procedural chunks all over Rails.

If you have an application that lends itself well to being on a single page (eg, lots of shared elements like navigation/sidebars/etc between pages for example), then it's going to be a pretty good fit for something like Backbone.

Your Rails app can act more like an API which any number of front-ends can use - the Backbone one for the web, an iPhone app, and so on.

There are surely downsides, too. You have to write at least some of your application's view/controller logic in Java(/Coffee)Script, and you have to be careful, when you don't reload the page for long periods, not to cause memory leaks and javascript errors that can kill your app. When is it not about tradeoffs, right?

A high profile example of someone not using Backbone when it otherwise might have been considered a good idea can be found at this 37signals post about Basecamp Next (and the corresponding HN discussion).

Let me know if I can clarify anything here for you.

rfunduk
  • 30,053
  • 5
  • 59
  • 54