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:
- For
Collection
sget
means something different than forModel
s. There is noset
. 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? - Assigning initial values inside
Model.extend
doesn't always work. For example assigningurl
will not override the default behaviour. This can only be achieved through a call toset()
method. Again very error prone. - I still don't know whether it's required to use
get
/set
insideinitialize()
call. - I don't understand why I can't just call
_.bindAll(this)
insideinitialize()
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 fram开发者_高级运维ework 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:
- Am I ever allowed to place additional attributes on extension like this:
var SomeModel = Backbone.Model.extend({ myattribute: myvalue })
?- If so, then why don't subsequent calls to
new SomeModel().get("myattribute")
work ?
- If so, then why don't subsequent calls to
- What exactly is
this
insideinitialize()
? 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.
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:
- Getting rid of the dreaded
Collection
s. "class methods" are used instead, - Getting most out of js prototypical nature (i.e. no
get
/set
is needed). Attributes are accessed directly. An explicit call tosave()
is required in order to trigger an event. Views
andControllers
are now merged into new type ofControllers
together whose purpose is to respond to DOM events and bind to model events.- The name :)
I find those design decisions coherent and sensible.
The reason there is no 'set' for Collections is because Collections are not arrays, they are sets, which are potentially ordered. The only supported way to place an element at a particular position is to add it to the collection and then sort the collection.
精彩评论