backbone.js 有哪些命名约定?

发布于 2024-12-23 11:16:22 字数 46 浏览 1 评论 0原文

我知道这并不重要,但我只是好奇人们如何命名他们的模型/集合/视图/路由器功能。

I know it is not that important, but I am just curious as to how people name their models / collections / views / router functions.

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

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

发布评论

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

评论(2

烟沫凡尘 2024-12-30 11:16:22

这一切都取决于模型、集合等代表什么以及它们在结构上如何使用。

我通常给我的模型一个通用名称,然后为所有其他模型添加其构造函数类型的后缀

Ex:

//  model
var Animal = Backbone.Model.extend({});....
//  collection
var AnimalCollection = Backbone.Collection......
// view
AnimalView = Backbone.View.extend(){})....

现在,您只需传递它的模型即可创建一个新的动物视图,并且其他阅读您代码的人可以轻松理解该模型代表什么。

var view = new AnimalView({ model: new Animal({name: 'Tiger'});

当然,您也可以随时在模型后加上 Model 一词,但我认为没有它就足够好了,因为模型代表具有属性的单个平面对象,而不是某种视图或集合。对象。

That all depends on what the model, collection, etc represents and how they are used structurally.

I typically give my models a generic name and then suffix all others with their constructor type

Ex:

//  model
var Animal = Backbone.Model.extend({});....
//  collection
var AnimalCollection = Backbone.Collection......
// view
AnimalView = Backbone.View.extend(){})....

Now you can create a new animal view simply by passing it it's model and other people who read your code can easily understand what the model represents.

var view = new AnimalView({ model: new Animal({name: 'Tiger'});

Of course, you can always suffix your models with the word Model as well, but I think it speaks well enough without it since a model represents a single flat object with properties not some sort of view or collection of objects.

顾挽 2024-12-30 11:16:22
  • 所有集合名称都以复数形式命名,例如 people/doors/doorlist。
  • 类以大写字母开头,实例使用驼峰/下划线。
  • 切勿在名称中包含型号、系列等词语。
  • 将所有内容放入名为 c、m、r、v 的哈希中,这有迭代的好处。
var c.People = Backbone.Collection.extend();
var c.people = new c.People();

var m.Person = Backbone.Model.extend();
var m.person = new m.Person();
  • All collection names plural somehow, like people/doors/doorlist.
  • Classes begin with capital, instances use camel/underscore.
  • Never include the word model, collection, etc. in name.
  • Put all in a hash called c, m, r, v which has benefit of iteration.
var c.People = Backbone.Collection.extend();
var c.people = new c.People();

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