0
votes

Trying to figure out how to use relationships and it just is not working.

// Data
{
    "apps": {
        "-AFCH5-Kvkc_nfQxnpZ8": {
            "name": "Leap Day",
            "playLink": "https://play.google.com/store/apps/details?id=com.nitrome.leapday",
            "price": "0.00",
            "like": 0,
            "show": "-SFCH5-Kvkc_nfQxnpZ8",
            "provider": "-PFCH5-Kvkc_nfQxnpZ8",
            "imageUrl": "https://www.google.com/url?q=https://lh3.googleusercontent.com/5oR-jbrmKNWqdWQnwrDjPD2PJUJekGK_BUAQOjKD3GuJRTk2MLVzuU2HJ0wyY2BYPsdS%3Dw300-rw&sa=D&ust=1464126095843000&usg=AFQjCNH0RSH6_0bA_EUqnZaldtUxCH1fAw"
        },
        "-AFCH5-Kvkc_nfQxnpZ9": {
            "name": "Gangfort",
            "playLink": "https://www.google.com/url?q=https://play.google.com/store/apps/details?id%3Dcom.gangfort.game.android&sa=D&ust=1464126095844000&usg=AFQjCNFSTZ1p_uBvIHYw97c29XwJU3gEjw",
            "price": "1.99",
            "like": 0,
            "show": "-SFCH5-Kvkc_nfQxnpZ8",
            "provider": "-PFCH5-Kvkc_nfQxnpZ8",
            "imageUrl": "https://www.google.com/url?q=https://lh3.googleusercontent.com/DV5mFhDQ2ADEbiF0S4cxL313JDqRazy9et7Etky5WtH7gxsm9DvbHhb52N0MH1swgfzR%3Dw300-rw&sa=D&ust=1464126095844000&usg=AFQjCNEymzxjrdN8wwL4qN40w5i8i9MlPw"
        }
    },
    "shows": {
        "-SFCH5-Kvkc_nfQxnpZ8": {
            "number": 432,
            "name": "Google I/O Secrets Revealed",
            "date": "05/22/2016",
            "app": ["-AFCH5-Kvkc_nfQxnpZ8", "-AFCH5-Kvkc_nfQxnpZ9"],
            "url": "http://podnutz.com/aaa432/"
        }
    },
    "providers": {
        "-PFCH5-Kvkc_nfQxnpZ8": {
            "firstName": "Steve",
            "lastName": "McLaughlin",
            "nick": "D2d",
            "app": ["-AFCH5-Kvkc_nfQxnpZ8", "-AFCH5-Kvkc_nfQxnpZ9"]
        }
    }
}

Here is the app model import Model from 'ember-data/model'; import attr from 'ember-data/attr'; import { belongsTo } from 'ember-data/relationships';

export default Model.extend({
  name: attr('string'),
  playLink: attr('string'),
  price: attr('string'),
  like: attr('number'),
  show: belongsTo('show', {
    async: true
  }),
  provider: belongsTo('provider', {
    async: true
  }),
  imageUrl: attr('string')
});

Here is the show model

import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { hasMany } from 'ember-data/relationships';

export default Model.extend({
  number: attr('number'),
  name: attr('string'),
  date: attr('date'),
  apps: hasMany('app'),
  url: attr('string')
});

Here is my route

import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    return this.store.findAll('app');
  }
});

Here is what the ember console looks like

Ember Console

So the problem is when I try to access the show object in my template, its not coming up

{{#each model as |show|}}
    {{app.show.name}}
{{/each}}

Update

From the looks of the markup it seems like something is working here

enter image description here

Here is the console when I log app.show

enter image description here

3
What is not working?user663031
Thanks, forgot to add that part. Updated postjrock2004
Please add also your router so that we see how the 'model' property is set (model() hook). According to the Ember console it seems the show instance is present, my guess is that your model is actually the app instance. try this: {{#if model.show}} {{model.show.name}} {{/if}}Pavol
Added route to original postjrock2004

3 Answers

0
votes

Looks like apps: hasMany('app') reference in your show model is overwriting your relationship defined in the shows model.

0
votes

you are not iterating through each app model in template. it should be like that

{{#each model as |app|}}
    {{app.show.name}}
{{/each}}

your each loop is like that {{#each model as |show|}}

0
votes

Try this:

{{#each model as |app|}}
  {{#if app.show}}
   {{app.show.name}}
  {{/if}}
{{else}}
   Loading apps...
{{/each}}

Here I would recommend taking advantage of ember-promise helpers since the relation between an app and a show is asynchronous:

{{#each model as |app}}
  {{#if (await app.show)}}
   {{get (await app.show) 'name'}}
  {{/if}}
{{else}}
   Loading apps...
{{/each}}