mardi 14 février 2017

How to define relationship between two same models?

Here is the two distincts payload (from Ember mirage) i'm trying to populate in my model:

# /contact/threads #index
[
  <Contact::Thread id: 1, thread_id: 1, content: 'lorem ...'>,
  <Contact::Thread id: 5, thread_id: 2, content: 'lorem ...'>,
  <Contact::Thread id: 6, thread_id: 3, content: 'lorem ...'>
]

# /contact/threads/1 #show
<Contact::Thread
  id: 2,
  thread_id: 1
  threads: [
    <Contact::Thread id: 2, thread_id: 1, content: 'lorem...'>
    <Contact::Thread id: 4, thread_id: 1, content: 'lorem...'>
    <Contact::Thread id: 7, thread_id: 1, content: 'lorem...'>
  ]
>

As you can see, there is a relationship between the same models 'Thread' (#show).

Now here is how I built Thread model on Ember side :

import DS from 'ember-data';
import { hasMany, belongsTo } from 'ember-data/relationships';

const { attr } = DS;

export default DS.Model.extend({
  threadId     : attr('number'),
  content      : attr('string'),

  threads      : hasMany('contact/threadMessage'),
  parent       : belongsTo('contact/threadMessage', {inverse: 'threads'}),
});

Everything seems to work except into 'threads'. I've got an array of undefined models :

thread.get('threads').toArray() _internalModel

Aucun commentaire:

Enregistrer un commentaire