0
votes

I've been using EmberModel in a working application for awhile, and now want to move back to using EmberData. I noticed from the "Model Guide" documentation on the Ember website that the proper way of defining a hasMany relationship in an EmberData model has changed. In earlier versions of EmberData, I defined my models like this:

 App.StratItem = DS.Model.extend({
   id: DS.attr('string),
   name: DS.attr('string'),
   quantity: DS.attr('number'),
   type: DS.attr('string'),
   strategy: DS.belongsTo('strat')
 });

 App.Strat = DS.Model.extend({
    id: DS.attr('string'),
    stratName: DS.attr('string'),
    sDate: DS.attr('number'),
    eDate: DS.attr('number'),
    stratItems: DS.hasMany('stratItem',{async:true})
 });

If I interpret the current documentations correctly, the correct definition for the same models now should be:

 App.StratItem = DS.Model.extend({
   id: DS.belongsTo('strat')
   name: DS.belongsTo('strat')
   quantity: DS.belongsTo('strat')
   type: DS.belongsTo('strat')
 });

 App.Strat = DS.Model.extend({
    id: DS.attr(),
    stratName: DS.attr(),
    sDate: DS.attr(),
    eDate: DS.attr(),
    stratItems: DS.hasMany('stratItem')
 });

Here are my questions:

1) Is my interpretation of the documentation correct?

2) If yes, what's the purpose of specifying "belongsTo" for every variable in "StratItem" while I've already specified in 'Strat' that the entire 'StratItem' model (i.e., all variables) is a child of 'Strat'?

3) Is {async:true} still necessary?

I'm getting an "error while loading route" error message from Ember on code that used to work with older versions of Ember (version 1.0 rc) and Ember Data; the above changes are the only ones I made so far.

1

1 Answers

0
votes

Not sure where you got confused, but you should not be defining a belongsTo relationship for attributes:

App.StratItem = DS.Model.extend({
   name: DS.attr(),
   quantity: DS.attr(),
   type: DS.attr(),
   strategy: DS.belongsTo('strat')
 });

 App.Strat = DS.Model.extend({
    id: DS.attr(),
    stratName: DS.attr(),
    sDate: DS.attr(),
    eDate: DS.attr(),
    stratItems: DS.hasMany('stratItem')
 });

id: DS.attr(), is unnecessary. Ember data does this automatically.

{async:true} is only necessary if you're not sideloading your related data.