mercredi 24 juin 2015

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?




Aucun commentaire:

Enregistrer un commentaire