Ember数据,关系和JSON

时间:2015-05-14 08:37:28

标签: javascript ember.js ember-data

我从API返回了以下JSON:

{
"project": [
    {
        "id": "1",
        "name": "My First Project",
        "owned_by": "1",
        "updated_at": {
            "date": "2015-05-06 15:46:27.000000",
            "timezone_type": 3,
            "timezone": "Europe/London"
        },
        "created_at": {
            "date": "2015-05-06 15:46:27.000000",
            "timezone_type": 3,
            "timezone": "Europe/London"
        }
    }
],
"subscriptions": [
    {
        "id": "10",
        "output": "Hello World",
        "project_id": "1",
        "owned_by": "1",
        "updated_at": {
            "date": "2015-05-06 16:56:40.000000",
            "timezone_type": 3,
            "timezone": "Europe/London"
        },
        "created_at": {
            "date": "2015-05-06 16:56:40.000000",
            "timezone_type": 3,
            "timezone": "Europe/London"
        }
    }
]
}

我有以下型号:

project.js

import DS from 'ember-data';

export default DS.Model.extend({
    name: DS.attr('string'),
    ownedBy: DS.attr('string'),
    subscriptions: DS.hasMany('subscription')
});

subscription.js      从'ember-data'导入DS;

 export default DS.Model.extend({
     input: DS.attr('string'),
     projects: DS.belongsTo('project'),
 });

数据正在从API成功返回,我可以获得项目详细信息,但我似乎无法获取关系数据。使用ember检查器在数据选项卡中显示数据,但是当我尝试使用{{#each item in model.subscription}}循环访问此数据时,不会返回任何内容。

enter image description here

1 个答案:

答案 0 :(得分:1)

您的项目json没有任何对其订阅的引用。项目json应该有一个id数组,每个id与订阅ID有关 下面的示例代码来自emberjs.com,但在项目中用项目代替帖子和订阅评论

{
  "post": {
    "id": 1,
    "title": "Node is not omakase",
    "comments": [1, 2, 3]
  },

  "comments": [{
    "id": 1,
    "body": "But is it _lightweight_ omakase?"
  },
  {
    "id": 2,
    "body": "I for one welcome our new omakase overlords"
  },
  {
    "id": 3,
    "body": "Put me on the fast track to a delicious dinner"
  }]
}