Backbone.js 中混合集合的最佳设计模式

发布于 2024-12-22 20:02:50 字数 1117 浏览 1 评论 0原文

我有一个项目集合,它们都共享一些数据(如 ID、标题),但是除了共享的属性之外,它们在功能上是唯一的项目,并且具有单独的视图和业务逻辑。

我的问题是没有 Backbone 风格 MVC 的经验,我不知道每个的优点/缺点......或者也许我缺少一个更优雅的解决方案。这是我可能会使用的 3 种技术的示例?

var gizmoCollection = new Backbone.Collection(); // or extend
var gizmoModel = Backbone.Model.extend({ ... });

var morgView = Backbone.View.extend({ ... });
var blarView = Backbone.View.extend({ ... });


// 1.)  Create an attribute for the view in the model?
gizmoCollection.add(new gizmoModel({ title: 'Gizmo1': view: morgView }));
gizmoCollection.add(new gizmoModel({ title: 'Gizmo2': view: blarView }));


// 2.)  Or create a seperate model for each type of model?
var morgModel = morgModel.extend({});
var blarModel = blarModel.extend({});

gizmoCollection.add(new morgModel({ title: 'Gizmo1' });
gizmoCollection.add(new blarModel({ title: 'Gizmo2' });


// 3.  Or register 'types' of views?
gizmoView.subClassView('morg', morgView);
gizmoView.subClassView('blar', blarView);
gizmoCollection.add(new gizmoModel({ title: 'Gizmo1', type: 'morg' });
gizmoCollection.add(new gizmoModel({ title: 'Gizmo2', type: 'blar' });

I have a collection of items they all share some data (like an id, title) however outside of their shared stem of attributes they're functionally unique items and have separate views and business logic.

My problem is without prior experience in Backbone style MVC, I don't know the pros / cons of each... or perhaps if there is a much more elegant solution I'm missing. Here's an example of the 3 techniques I could potentially use?

var gizmoCollection = new Backbone.Collection(); // or extend
var gizmoModel = Backbone.Model.extend({ ... });

var morgView = Backbone.View.extend({ ... });
var blarView = Backbone.View.extend({ ... });


// 1.)  Create an attribute for the view in the model?
gizmoCollection.add(new gizmoModel({ title: 'Gizmo1': view: morgView }));
gizmoCollection.add(new gizmoModel({ title: 'Gizmo2': view: blarView }));


// 2.)  Or create a seperate model for each type of model?
var morgModel = morgModel.extend({});
var blarModel = blarModel.extend({});

gizmoCollection.add(new morgModel({ title: 'Gizmo1' });
gizmoCollection.add(new blarModel({ title: 'Gizmo2' });


// 3.  Or register 'types' of views?
gizmoView.subClassView('morg', morgView);
gizmoView.subClassView('blar', blarView);
gizmoCollection.add(new gizmoModel({ title: 'Gizmo1', type: 'morg' });
gizmoCollection.add(new gizmoModel({ title: 'Gizmo2', type: 'blar' });

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

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

发布评论

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

评论(1

知你几分 2024-12-29 20:02:50

我的选择是创建单独的模型,并在必要时查看它。原因是每个模型都应该拥有自己的业务逻辑。现在,您可能会发现,如果只有每个模型类型或模型属性值都不同的表示逻辑,则有时仅使用子视图可以更轻松地执行此操作。

您应该记住以下内容:

  1. 表示逻辑转到演示者(Backbone.View)
  2. 业务逻辑转到模型(Backbone.Model)
  3. 导航逻辑可以是路由器(也称为控制器),也可以从 Backbone 创建事件总线.Events 或 jQuery.callbacks() 将完成这项工作,并且可能还有一些您希望与演示者和模型分开的其他事情。

最后一点。始终记住,您的应用程序会不断增长,有时,无论您目前不需要那么多复杂性,再添加几行代码是更明智的做法。但是,如果您感觉代码在某个时候会变得更加复杂,那么您应该立即执行此操作,否则稍后您将没有足够的时间。

My choice would be to create separate models and it views if necessary. The reason is that each model should hold business logic for it self. Now, you may find sometimes easier to do this just with subviews if there is only presentational logic which is different for each model type or model attribute value.

You should keep in mind following:

  1. Presentational logic goes to Presenter(s) (Backbone.View)
  2. Business logic goest to model(s) (Backbone.Model)
  3. Navigation logic either router (aka controller) or you can make your Event Bus from Backbone.Events or jQuery.callbacks() which will do this job and probably some other things which you want separate from your presenters and models.

Final note. Always keep in mind that your app will grow, sometimes it is wiser to add few more lines of code regardless you don't need so much complexity at the moment. But if you senses tell you that at some point that code will become more complex, well you should do it right away or later you will not have enough time.

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