2012-08-03 26 views
13

Hãy nói rằng đường ray mô hình của tôi trông như thế này:Làm thế nào để phân lớp hoặc thừa kế một mô hình từ mô hình khác sử dụng ember dữ liệu

class SalesRelationship < ActiveRecord 

end 

Đó là thừa hưởng bởi crossSell như thế này:

class crossSell < SalesRelationship 

end 

Làm cách nào để hiển thị mối quan hệ kế thừa này trong dữ liệu ember. thực hành tốt nhất cho việc này là gì:

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

Tôi có thể tạo một lớp con gọi là 'crossSell', như thế này

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

hay như thế này

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

Trả lời

13

Khá gần, bạn chỉ có thể mở rộng SalesRelationship.

App.CrossSell = App.SalesRelationship.extend({ 
    productName: DS.attr('string') 
}) 
6

Trong Ember 2.7, bạn có thể làm như thế này. Giả sử bạn có một lớp Person và muốn kế thừa từ nó để làm cho một Employee cho trường trạng thái (như thuê, đã về hưu, bị sa thải trên rời vv)

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(), 
}); 
+1

Cách 'nhập người từ './person';' thay vì thấy cả hai mô hình đều nằm trong cùng một thư mục. – Caltor

+1

@Caltor Cảm ơn, tôi đã cập nhật mã :) Tốt bắt! – rmcsharry

+0

Hoàn hảo, cảm ơn rất nhiều! –

Các vấn đề liên quan