在Ember中,camelcase模型关系的惯例是什么?

时间:2014-04-16 15:01:19

标签: ember.js model relationship

这实际上是关于Ember.js中模型关系的两部分问题

首先,由于ember网站上的示例使用了这个约定,看起来公约可能已经改变了1.0的正式版本:

App.Post = DS.Model.extend({
  comments: DS.hasMany('comment')
});

App.Comment = DS.Model.extend({
  post: DS.belongsTo('post')
});

但是我看到很多去年代码的例子:

App.Post = DS.Model.extend({
  comments: DS.hasMany('App.Comment')
});

App.Comment = DS.Model.extend({
  post: DS.belongsTo('App.Post')
});

所以,第1部分是:此约定是否已更改为当前Ember网站上的内容,或者是否有人使用'App.Comment'的原因?

第2部分与使用驼峰式模型名称时的约定有关。

假设如下:

App.Category = DS.Model.extend({
  //attributes
});

App.SubCategory = DS.Model.extend({
  //attributes
});

我想知道建立关系的惯例是什么:

App.Category = DS.Model.extend({
  sub_categories: DS.hasMany('sub_categories')
  // or
  sub_categories: DS.hasMany('subCategories')
  // or
  subCategories: DS.hasMany('sub_categories')
  // or
  subCategories: DS.hasMany('subCategories')
  // or
  sub_categories: DS.hasMany('App.SubCategory')
  // or
  subCategories: DS.hasMany('App.SubCategory')
  // attributes
});

我的问题的第2部分是:上述哪种写关系方式是惯例?或者还有其他一些我没想过的方法吗?

1 个答案:

答案 0 :(得分:1)

  

此约定是否已更改为目前Ember网站上的内容,或者是否存在人们正在使用的原因' App.Comment'?

这是一个很好的速记。

这一个。较低版本用于Javascript属性,但是蛇用于模型类型键。

  subCategories: DS.hasMany('sub_categories')