Well, I canβt decide for you, it depends mainly on how close the deadline is, but I prefer the Backbone.js approach.
If I have to argue, I can say that you will have static and cached JS scripts and lightweight AJAX requests (JSON only), while with a different approach you will load heavier and non-cached scripts.
But first of all, I believe that the Backbone method is the best way to get your code organized and maintained.
Coffeescript is awesome and very fast to learn. This simplifies JS syntax and makes it fun. It is worth a try. Studied in 30 million.
Backbone.js is awesome and fast to learn. Using this approach will allow you to rely on events that are much purer than we can do.
Thanks to the asset pipeline, you can separate the class views / models / routers in separate files, which is very nice.
Thanks to coffeescript, you can write your base objects using very clean syntax:
class @MyView extends Backbone.View events: 'click obj': 'handler' [...]
With this, I add a little @module
to my projects to organize my objects in namespaces.
However, it will take you some time to find a good file organization.
You can start with gem rails-backbone
and have some rail-like generators. I donβt like it, but I think this is a good start. It includes the Backbone.sync
function adapted to the rails.
Edit
Here are some details about the @module
. I include this in application.js.coffee
(don't forget require_self
):
@module = (names, fn) -> names = names.split '.' if typeof names is 'string' space = @[names.shift()] ||= {} space.module ||= @module if names.length space.module names, fn else fn.call space
In my class files:
@module 'MyProject.Model', -> class @MyModel extends Backbone.Model [...]
(Note that @
is a coffeescript shortcut for this.
.)
The assistant creates MyProject
and MyProject.Model
if necessary (if null) and performs the specified function with this
binding to MyProject.Model
. This way you can access your model as from the root namespace ( document
):
m = new MyProject.Model.MyModel
You can also bind an assistant:
@module 'MyProject', -> @module 'Model', -> [...]
I use the following namespace hierarchy
MyProject Model View Router Runtime (to store all runtimes objects and don't pollute the root namespace, easier for debug)