Question

This is really a two-part question regarding model relationships in Ember.js

First, it looks like the convention may have changed with the official release of 1.0, since the examples on the ember site use this convention:

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

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

But I see a lot of examples from last year where code looks like this:

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

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

So, part 1 is: Has this convention changed to what is on the Ember site currently, or are there reasons people are using 'App.Comment'?

Part 2 has to do with the convention when using camel cased model names.

Assuming the following:

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

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

I'm wondering what the convention is for setting up the relationships:

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
});

Part 2 of my question is: Which of the above ways of writing the relationship is the convention? Or is there some other way I am not thinking about?

Was it helpful?

Solution

Has this convention changed to what is on the Ember site currently, or are there reasons people are using 'App.Comment'?

It's a nice shorthand.

This one. lower-camelcased for Javascript attribute but snake cased for model type key.

  subCategories: DS.hasMany('sub_categories')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top