ember-数据关系不起作用

mrfwxfqh  于 2022-11-23  发布在  其他
关注(0)|答案(3)|浏览(130)

试图弄清楚如何利用关系,它只是不起作用。

// 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"]
        }
    }
}

下面是应用模型从'ember-data/model'导入模型;从'ember-data/attr'导入属性;从“成员数据/关系”导入{归属于};

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')
});

这是展示模型

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')
});

这是我的路线

import Ember from 'ember';

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

以下是ember控制台的外观

因此,问题是当我尝试访问模板中的show对象时,它没有出现

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

更新
从标记的外观来看,似乎有什么东西在这里工作

这是我登录www.example.com时的控制台app.show

zazmityj

zazmityj1#

看起来show模型中的apps: hasMany('app')引用正在覆盖显示模型中定义的关系。

lhcgjxsq

lhcgjxsq2#

您没有在模板中迭代每个应用程序模型。它应该是这样的

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

每个循环就像{{#each model as |show|}}

q8l4jmvw

q8l4jmvw3#

试试看:

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

这里我建议利用ember-promise helpers,因为应用程序和节目之间的关系是异步的:

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

相关问题