Backbone.js 及其 API 混乱

发布于 2024-11-03 09:25:15 字数 1716 浏览 2 评论 0原文

我最近开始使用 Backbone.js。我喜欢这个架构,就功能而言,它几乎正是我所需要的......

但是我发现了以下警告:

  • 对于 Collection s get 意味着与对于模型。没有设置。应以常规方式访问属性。我觉得这很不一致。有时很容易混淆模型和集合。有什么办法可以克服这个问题吗?
  • Model.extend 中分配初始值并不总是有效。例如,分配 url不会覆盖默认行为。这只能通过调用 set() 方法来实现。再次非常容易出错。
  • 我仍然不知道是否需要在 initialize() 调用中使用 get/set
  • 我不明白为什么我不能只在 initialize() 中调用 _.bindAll(this) 并且我必须列出要绑定的特定函数名称,如下所示: _.bindAll(this,firstFunc,secondFunc,...)。这不是很干。

我想知道:针对上述情况,最佳做法是什么?你如何使框架更加一致——任何猴子补丁?我做错了什么/违反惯例吗?

如果有任何现实世界中的好例子,我将不胜感激。我确实找到了这个: http://documentcloud.github.com/backbone/docs/todos.htmlhttp://liquidmedia.ca/blog/2011/01/backbone-js-part- 1/ 并且这些并没有解决任何提到的问题。事实上,它们只是提出了最简单的想法,绝对没有边界情况,所以任何更复杂的东西都可能有用。

编辑:

好的,还有一个我不明白的更基本的想法:

  • 我是否可以在扩展上放置额外的属性,如下所示:var SomeModel = Backbone.Model.extend({ myattribute: myvalue }) ?
    • 如果是这样,那么为什么后续调用 new SomeModel().get("myattribute") 不起作用?
  • initialize() 中的 this 到底是什么?是模型类还是模型实例?

编辑(2):

嗯,我发现了这个:http://maccman.github.com/spine/< /a>.它看起来像 Backbone.js 2.0,也有相似的名称:)。尚未对其进行测试,这可能会有点阻碍,因为该库非常是最新的。然而,从文档方面来看,它看起来非常有希望。它消除了我发现的大部分问题,简化了 API,甚至消除了对 underscore.js 的依赖,这对于库来说是一件好事。我将在这里发布我的进一步发现。

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 Collections get means something different than for Models. 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?
  • Assigning initial values inside 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.
  • I still don't know whether it's required to use get/set inside initialize() call.
  • I don't understand why I can't just 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:

  • 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 ?
  • What exactly is 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

脸赞 2024-11-10 09:25:15

好吧,我想我现在可以相当自信地说:Backbone 已死,Spine 万岁。

Spine 并不完全是 Backbone 的分支。然而,它非常相似,并且明显受到一些设计决策的启发。可以说,作者试图尽可能保留原来的主干 API,去掉所有不必要或不合逻辑的东西。我发现它也更容易扩展。更改列表包括:

  • 摆脱可怕的Collection。而是使用“类方法”,
  • 充分利用 js 原型性质(即不需要 get/set)。直接访问属性。需要显式调用 save() 才能触发事件。
  • ViewsControllers 现在合并为新类型的 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:

  • Getting rid of the dreaded Collections. "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 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.
  • The name :)

I find those design decisions coherent and sensible.

半透明的墙 2024-11-10 09:25:15

集合没有“集合”的原因是因为集合不是数组,它们是集合,并且可能是有序的。将元素放置在特定位置的唯一受支持的方法是将其添加到集合中,然后对集合进行排序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文