Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4829

belongsTo data of the model isn't loaded

$
0
0

@dca123 wrote:

Here is my model: loan.js

import DS from 'ember-data';
export default DS.Model.extend({
  notes: DS.attr('string', {defaultValue: ''}),
  returned: DS.attr('boolean'),
  createdAt: DS.attr('date'),
  friend: DS.belongsTo('friend'),
  article: DS.belongsTo('article')
});

This is my model for article:

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

export default DS.Model.extend({
  name: DS.attr('string'),
  loans: hasMany('loan')
});

For my route loans/index this is what I have

import Ember from 'ember';

export default Ember.Route.extend({
  model(){
    return this.modelFor('friends.show').get('loans');
  }
});

Finally my loans/index.hbs is as such

<tbody>
    {{#each model as |loan|}}
    <tr>
      <td>
        {{log loan}}
        {{loan.article.name}}
      </td>
      <td>
        {{loan.notes}}
      </td>
      <td>
        {{loan.createdAt}}
      </td>
    </tr>
    {{/each}}
  </tbody>

Finally the response from the API

{

    "data": [
        {
            "id": "1",
            "type": "loans",
            "links": {
                "self": "http://ember-cli-devinda.herokuapp.com/loans/1"
            },
            "attributes": {
                "notes": "fgfhf",
                "returned": false,
                "created-at": "2017-08-08T11:22:25.136Z"
            },
            "relationships": {
                "article": {
                    "links": {
                        "self": "http://ember-cli-devinda.herokuapp.com/loans/1/relationships/article",
                        "related": "http://ember-cli-devinda.herokuapp.com/loans/1/article"
                    }
                },
                "friend": {
                    "links": {
                        "self": "http://ember-cli-devinda.herokuapp.com/loans/1/relationships/friend",
                        "related": "http://ember-cli-devinda.herokuapp.com/loans/1/friend"
                    }
                }
            }
        }
    ],
    "links": {
        "first": "http://ember-cli-devinda.herokuapp.com/friends/1/loans?page%5Bnumber%5D=1&page%5Bsize%5D=10",
        "last": "http://ember-cli-devinda.herokuapp.com/friends/1/loans?page%5Bnumber%5D=1&page%5Bsize%5D=10"
    }

}

When i include {{loan.article.name}} the console only prints out ‘loan’ once. This is my result when I convert loan to json with .toJSON()

If i take the ‘loan’ object, save it in the console (as temp0) and run temp0.get(‘article’).get(‘name’) it returns undefined. However if I run the very same code again in the console I get the desired result (the name of the article which belongs to said loan)!

I figured this might mean that when the page is loaded, the model (loan) doesn’t load its belongsTo(‘article’) data.

Thanks !

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4829

Trending Articles