Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subclass or inherit a model from another model using ember-data

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')
  });
like image 560
brg Avatar asked Aug 03 '12 10:08

brg


2 Answers

Pretty close, you can just extend SalesRelationship.

App.CrossSell = App.SalesRelationship.extend({
  productName: DS.attr('string')
})
like image 149
Bradley Priest Avatar answered Oct 18 '22 07:10

Bradley Priest


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(),
});
like image 8
rmcsharry Avatar answered Oct 18 '22 05:10

rmcsharry