Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js在同一文件中使用RESTAdapter访问不同的json属性_Json_Ember.js_Ember Data - Fatal编程技术网

Ember.js在同一文件中使用RESTAdapter访问不同的json属性

Ember.js在同一文件中使用RESTAdapter访问不同的json属性,json,ember.js,ember-data,Json,Ember.js,Ember Data,我有一个这样的模型: App.Person = DS.Model.extend({ name: DS.attr('string'), clothes: DS.attr() }); 还有一个名为这样的人的文件 { "persons": [ { "id": 1, "name": "person1", "clothes": ["one trouser","another trouser"] }, { "id": 2, "name":

我有一个这样的模型:

App.Person = DS.Model.extend({
name: DS.attr('string'),
clothes: DS.attr()
});
还有一个名为这样的人的文件

{
  "persons": [
    {
    "id": 1,
    "name": "person1",
    "clothes": ["one trouser","another trouser"]
    },
    {
    "id": 2,
    "name": "person2",
    "clothes": ["one trouser","another trouser"]
    }
   ]
}
我称之为:

return this.store.find('person');
现在,我想在json文件中添加例如关于谁制作了裤子的信息:

"trousers": [
  {
   "id": 1,
   "name": "trouser1",
   "madeby": ["one worker","another worker"]
  },
  {
   "id": 2,
   "name": "trouser2",
   "clothes": ["one worker","another worker"]
  }
]

我不是在问如何建立两者之间的关系,而是如何将它们放在同一个json文件中,并调用每个json文件。

我想你说的是侧面加载。Ember数据可以处理JSON中的多种类型的实体

假设你有一条裤子的模型

如果要像这样返回JSON:

{
  "people": [
    {
      "id": 1,
      "name": "person1",
      "clothes": [1, 2]
    },
    {
      "id": 2,
      "name": "person2",
      "clothes": [1, 2]
    }
  ],
  "trousers": [
    {
      "id": 1,
      "name": "trouser1",
      "madeby": ["one worker","another worker"]
    },
    {
     "id": 2,
     "name": "trouser2",
     "clothes": ["one worker","another worker"]
    }
  ]
}
然后一个store.find'person'会看到裤子的钥匙,并自动将其加载到裤子模型下的商店中。类似地,如果您有另一个名为Pet的模型,您可以向JSON添加另一个键,以自动加载一些Pet:

{
  "persons": [
    // ...
  ],
  "trousers": [
    // ...
  ],
  "pets": [
    // some pets here
  ]
}
即使您没有在所有这些模型之间创建关系,将它们放在JSON中也会将它们加载到ember数据的存储中

一旦它们进入存储区,就可以通过id进行标准查找:

然后,您可以通过执行以下操作来建立关系:

App.Person = DS.Model.extend({
  name: DS.attr('string'),
  clothes: DS.hasMany('trouser') // <--
});

这个。商店。找“裤子”,1;如果调用GET-localhost/Bearts而不是personsIt返回的同一个json文件,我如何从那里访问该名称。get'name'在我使用时不起作用。然后它返回一个[object object],如果y尝试使用each查看它:each循环的值必须是一个数组。您通过了{u id:111,{u标签:未定义,{u订户:}
this.store.find('trouser', 1);
App.Person = DS.Model.extend({
  name: DS.attr('string'),
  clothes: DS.hasMany('trouser') // <--
});