14

Let's say my rails models look like this:

class SalesRelationship < ActiveRecord

end

Which is inherited by crossSell like this:

class crossSell < SalesRelationship 

end

How do I show this inheritance relationship in ember-data. What is the best practise for this:

App.salesRelationship = DS.Model.extend({
  name: DS.attr('string')
});

Can I create a subclass called 'crossSell', like this

crossSell = App.salesRelationship({
    productName: DS.attr('string')
});

or like this

 App.salesRelationship.crossSell  = DS.Model.extend({
    productName: DS.attr('string')
  });
David Gardiner
  • 16,892
  • 20
  • 80
  • 117
brg
  • 3,915
  • 8
  • 37
  • 66

2 Answers2

13

Pretty close, you can just extend SalesRelationship.

App.CrossSell = App.SalesRelationship.extend({
  productName: DS.attr('string')
})
Bradley Priest
  • 7,438
  • 1
  • 29
  • 33
8

In Ember 2.7 it can be done like this. Assume you have a Person class and wish to inherit from it to make an Employee for a status field (like hired, retired, fired on-leave etc)

app/models/person.js

import DS from 'ember-data';

export default DS.Model.extend({
  firstName: DS.attr(),
  lastName: DS.attr(),
  fullName: Ember.computed('firstName', 'lastName', function() {
    return `${this.get('lastName')}, ${this.get('firstName')}`;
});

app/models/employee.js

import DS from 'ember-data';

import Person from './person';

export default Person.extend({
  status: DS.attr(),
  statusCode: DS.attr(),
});
rmcsharry
  • 5,363
  • 6
  • 65
  • 108
  • 1
    How about `import Person from './person';` instead seeing as both models are in the same folder. – Caltor Jan 17 '17 at 15:02