dimanche 14 août 2016

Whats the proper way to load a model with all of its associated models in Ember.js

I'm pretty new to Ember and I'm trying to associate my house model with my expense model. However whenever I add expenses: DS.hasMany('expense') to my house model, I get ember.debug.js:32096 TypeError: Cannot read property 'some' of undefined in Ember Inspector. I was able to successfully define the relationship between my house and my unit. I'm not sure what I'm missing...

House has many units. House has many expenses. Unit belongs to House. Unit has many Expenses.

Models:

//House Model/////////
export default DS.Model.extend({
  street_name: DS.attr('string'),
  num_units: DS.attr('number'),
  purchase_price: DS.attr('number'),
  current_price: DS.attr('number'),
  mortgage: DS.attr('number'),
  taxes: DS.attr('number'),
  insurance: DS.attr('number'),
  units: DS.hasMany('unit'),
  expenses: DS.hasMany('expense') <- If I remove this, then the error goes away. 
});


//Unit Model/////////
export default DS.Model.extend({
  unit_num: DS.attr('string'),
  num_bedrooms: DS.attr('number'),
  num_bathrooms: DS.attr('number'),
  num_parking: DS.attr('number'),
  expense: DS.attr('number'),
  house: DS.belongsTo('house'),
  contract: DS.belongsTo('contract'),
  expenses: DS.hasMany('expense')
});


 //Expense Model/////////
 export default DS.Model.extend({
  expense_for: DS.attr('string'),
  expense_description: DS.attr('string'),
  expense_date: DS.attr('date'),
  expense_amount: DS.attr('integer'),
  house: DS.belongsTo('house'),
  unit: DS.belongsTo('unit')
});

House Serializer

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    units: { embedded: 'always' },
    // expenses: { embedded: 'always' }, <- I tried embedding the expense model, but it didn't work
  }
});

I'm using Active Model Adapter and Active Model Serializer. I'm using Ember-cli 2.5.1 with a Rails 4 API that I wrote.

I've been stuck on this for awhile now and I'll appreciate any help I can get. Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire