EchtFettigerKeks
EchtFettigerKeks

Reputation: 1893

Ember.js: Inherit models with belongsTo same type

I want to use a self referencing model like that

//models/instanceable-element.js
import DS from 'ember-data';

export default DS.Model.extend({
  instanceName: DS.attr('string'),
  parentElement: DS.belongsTo('instanceable-element', {async: true})
});


//models/basic-element.js
import DS from 'ember-data';
import InstanceableElement from './instanceable-element';

export default InstanceableElement.extend({
  name: DS.attr('string'),
  description: DS.attr('string'),
});


//models/alpha.js
import DS from 'ember-data';
import BasicElement from './basic-element';

export default BasicElement.extend({
  someMember: DS.attr('string')
});

Visual:

                +-------------+                     
                |             |                     
                |             |                     
                |             ++parentElement       
+--------------------+        |belongsTo("sameType")
|InstanceableElement |        |                     
|                    <--------+                     
|                    |                              
|                    |                              
+---------^----------+                              
          |                                         
          |                                         
          |inherit                                  
          |                                         
          |                                         
+--------------------+                              
|  BasicElement      |                              
|                    |                              
|                    |                              
|                    |                              
+---------^----------+                              
          |                                         
          |inherit                                  
          |                                         
+--------------------+                              
|  Alpha             |                              
|                    |                              
|                    |                              
|                    |                              
+--------------------+                              

The idea is that every alpha (and other models which inherit from basic-elment->instanceable-elment) can have sub-items of the same type, like alpha can have a alpha as a parent. So now, when Im requesting from an alpha the parent Element with alpha.get('parentElement'), the RESTAdaper going to call something like that:

GET http://localhost:4200/api/instanceableElements/3 404 (Not Found)

But I want that the request would go to a

GET http://localhost:4200/api/alphas/3

how can I do that?

Upvotes: 0

Views: 68

Answers (1)

user663031
user663031

Reputation:

You're trying to do something resembling polymorphism. You've found a basic problem with a simple implementation using a model superclass. You could try using Ember Data's built-in polymorphism support, but it's spotty. You can read the docs about polymorphism and try to figure it out. For instance, your server will have to return the associations in a special way, as

parent: { type: 'alpha', id: 123 }

But I don't predict much joy from going this route. Unless you want to spend more time than you probably have, you would be better off giving up on your laudable goal of elegantly encapsulating the parent relation in a superclass and just putting class-specific parents in each class.

Upvotes: 1

Related Questions