I've recently started using Backbone.js
. I like the architecture, in terms of features it's almost exactly what I need...
... However I found the following caveats:
Collection
s get
means something different than for Model
s. There is no set
. Attributes should be accessed in a regular way. I find it rather inconsistent. It's easy to confuse models and collections sometimes. Is there anything that can be done to overcome this?Model.extend
doesn't always work. For example assigning url
will not override the default behaviour. This can only be achieved through a call to set()
method. Again very error prone.get
/set
inside initialize()
call._.bindAll(this)
inside initialize()
and I have to list specific function names to be bound like this: _.bindAll(this, firstFunc, secondFunc, ...)
. This is not very DRY.I would like to know: what are the best practices regarding the mentioned situations? What do you do to make the framework more consistent - any monkey patching? Am I doing anything wrong / against the convention?
I'd be grateful for any good real world examples. I did find this: http://documentcloud.github.com/backbone/docs/todos.html and http://liquidmedia.ca/blog/2011/01/backbone-js-part-1/ and those don't address any of the mentioned problems. In fact they just present the simplest ideas and absolutely no border cases, so anything more complicated could be useful.
EDIT:
Ok, and there is one more fundamental think I don't understand:
var SomeModel = Backbone.Model.extend({ myattribute: myvalue })
?
new SomeModel().get("myattribute")
work ?this
inside initialize()
? Is it model class or model instance ?EDIT(2):
Well, I found this: http://maccman.github.com/spine/. It looks like Backbone.js 2.0, shares a similar name too :). Haven't tested it yet, which might be a bit of a show stopper, as the library is very recent. However from the docs side of things it looks very promissing. It gets rid of most of the problems that I found, it simplifies the API, it even gets rid of the dependency on underscore.js
which for a library is a good thing. I'll post my further findings here.
Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.
Front-End MVC frameworks (Backbone, Angular, etc) all rely on a backend service to provide the data that, say Backbone, would then use as its model. You could have an entire MVC pattern on the backend that accepts requests and spits out some JSON for a frontend MVC framework to use.
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.
Vue. js, React, AngularJS, Angular 2, and Ember. js are the most popular alternatives and competitors to Backbone. js.
Ok, I think I can say it quite confidently now: Backbone is dead, long live Spine.
Spine isn't exactly a fork of Backbone. It is however very similar and clearly inspired by some of the design decisions. It could be said that the author tried to retain as much as it was possible the original backbone API, getting rid of everything unnecessary or illogical. I find it also easier to extend. The list of changes includes among other things:
Collection
s. "class methods" are used instead,get
/set
is needed). Attributes are accessed directly. An explicit call to save()
is required in order to trigger an event.Views
and Controllers
are now merged into new type of Controllers
together whose purpose is to respond to DOM events and bind to model events.I find those design decisions coherent and sensible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With