vendredi 31 mai 2019

Ember not setting hasMany relationship attribute after retrieving data

Foreward: There are a lot of firsts in this project, such as building my first API, first time using JSON API, first time using Ember, first time posting on SO, etc.

I'm trying to access an 'authors' hasMany attribute in my template containing the latest updates. What I access it in the template, however, nothing is returned. The models are being saved correctly, but the relationship is not being set it appears, as the length of the DS.PromiseManyArray on latest.contentAuthors is 0 while the promise is fulfilled ({ _length: 0, isFulfilled: true, isRejected: false }).

I'm using Ember 3.10 (with CLI). I have full control over my backend (LAMP running ExpressionEngine 5) and am serving API requests through a custom built plugin, though I'm not sure that matters to much as this seems to largely be a frontend problem from what I can discern.

Route

import Route from '@ember/routing/route';

export default Route.extend({
    model(){
        let latest = this.store.peekAll('latest');
        if (latest.length < 2){
            latest = this.store.query('latest', { limit: 2, include: "people" });
        }
        return latest;
    }
});

Base Model

import DS from 'ember-data';
const { Model } = DS;

export default Model.extend({
    title: DS.attr()
});

Latest Model

import DS from 'ember-data';
import ExpressionEngineBase from './expression-engine-base';

export default ExpressionEngineBase.extend({
    title: DS.attr(),
    blurb: DS.attr(),
    contentAuthors: DS.hasMany('person')
});

Person Model

import DS from 'ember-data';
import ExpressionEngineBase from './expression-engine-base';

export default ExpressionEngineBase.extend({
    title: DS.attr(),
    latest: DS.hasMany('latest')
});

Template


    <h2></h2>
    
        <div></div>
    
        <div>Can't find author(s)</div>
    
    <p></p>


Data sent from server

{
    "data": [{
        "id": 3161,
        "type": "latest",
        "attributes": {
            "title": "Amazing Video 1"
        },
        "links": {
            "self": "https:\/\/cms.example.com\/api\/v1\/video\/3161"
        },
        "relationships": {
            "people": {
                "data": [{
                    "id": 1,
                    "type": "people",
                    "links": {
                        "self": "https:\/\/cms.example.com\/api\/v1\/people\/1",
                        "channel": "https:\/\/cms.example.com\/api\/v1\/people"
                    }
                }]
            }
        }
    }, {
        "id": 2573,
        "type": "latest",
        "attributes": {
            "title": "Amazing Article 1"
        },
        "links": {
            "self": "https:\/\/cms.example.com\/api\/v1\/white_papers_insights\/2573"
        },
        "relationships": {
            "people": {
                "data": [{
                    "id": 1,
                    "type": "people",
                    "links": {
                        "self": "https:\/\/cms.example.com\/api\/v1\/people\/1",
                        "channel": "https:\/\/cms.example.com\/api\/v1\/people"
                    }
                }, {
                    "id": 52,
                    "type": "people",
                    "links": {
                        "self": "https:\/\/cms.example.com\/api\/v1\/people\/52",
                        "channel": "https:\/\/cms.example.com\/api\/v1\/people"
                    }
                }]
            }
        }
    }],
    "links": {
        "self": "https:\/\/cms.example.com\/api\/v1\/latest?include=people&limit=2"
    },
    "included": [{
        "id": 1,
        "type": "people",
        "links": {
            "self": "https:\/\/cms.example.com\/api\/v1\/people\/1",
            "channel": "https:\/\/cms.example.com\/api\/v1\/people"
        },
        "attributes": {
            "title": "Great Author"
        }
    }, {
        "id": 52,
        "type": "people",
        "links": {
            "self": "https:\/\/cms.example.com\/api\/v1\/people\/52",
            "channel": "https:\/\/cms.example.com\/api\/v1\/people"
        },
        "attributes": {
            "title": "Great Co-Author"
        }
    }]
}

To reiterate, the relationship model is saving and is viewable in Ember Inspector, but the actual link/relationship is not being set.




Aucun commentaire:

Enregistrer un commentaire