Backbone.js 及其 API 混乱
我最近开始使用 Backbone.js
。我喜欢这个架构,就功能而言,它几乎正是我所需要的......
但是我发现了以下警告:
- 对于
Collection
sget
意味着与对于模型
。没有设置
。应以常规方式访问属性。我觉得这很不一致。有时很容易混淆模型和集合。有什么办法可以克服这个问题吗? - 在
Model.extend
中分配初始值并不总是有效。例如,分配url
将不会覆盖默认行为。这只能通过调用set()
方法来实现。再次非常容易出错。 - 我仍然不知道是否需要在
initialize()
调用中使用get
/set
。 - 我不明白为什么我不能只在
initialize()
中调用_.bindAll(this)
并且我必须列出要绑定的特定函数名称,如下所示:_.bindAll(this,firstFunc,secondFunc,...)
。这不是很干。
我想知道:针对上述情况,最佳做法是什么?你如何使框架更加一致——任何猴子补丁?我做错了什么/违反惯例吗?
如果有任何现实世界中的好例子,我将不胜感激。我确实找到了这个: http://documentcloud.github.com/backbone/docs/todos.html和http://liquidmedia.ca/blog/2011/01/backbone-js-part- 1/ 并且这些并没有解决任何提到的问题。事实上,它们只是提出了最简单的想法,绝对没有边界情况,所以任何更复杂的东西都可能有用。
编辑:
好的,还有一个我不明白的更基本的想法:
- 我是否可以在扩展上放置额外的属性,如下所示:
var SomeModel = Backbone.Model.extend({ myattribute: myvalue })
?- 如果是这样,那么为什么后续调用
new SomeModel().get("myattribute")
不起作用?
- 如果是这样,那么为什么后续调用
initialize()
中的this
到底是什么?是模型类还是模型实例?
编辑(2):
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 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:
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我想我现在可以相当自信地说:Backbone 已死,Spine 万岁。
Spine 并不完全是 Backbone 的分支。然而,它非常相似,并且明显受到一些设计决策的启发。可以说,作者试图尽可能保留原来的主干 API,去掉所有不必要或不合逻辑的东西。我发现它也更容易扩展。更改列表包括:
Collection
。而是使用“类方法”,get
/set
)。直接访问属性。需要显式调用save()
才能触发事件。Views
和Controllers
现在合并为新类型的Controllers
,其目的是响应 DOM 事件并绑定到模型事件。我发现这些设计决策是连贯且明智的。
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 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.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.