初学者EmberJS:与ManyArray的关系不起作用

时间:2016-01-07 09:57:21

标签: javascript json ember.js server

我写信问你为什么我不能在我的对象中得到关系数据。

我解释说:

我创建了两个模型对象:用户和机构。两者之间的关系是多对多。

在服务器上(实际上是切换到当前正在开发的rails服务器之前/服务器上的嵌入式存根),关系是相同的。我的服务器发送关于JSON API约定的数据。顺便说一句,我在应用程序中使用的适配器扩展了JSONAPIAdapter。

所有数据都已正确填写:当我通过服务中的商店通过“查找”方法调用数据时,我可以正确填写我的用户模型,一切正常:

this.get('store').find('user', id);

用户返回得很好,我可以在应用程序中查看我的数据。

但是用户还拥有一系列“机构”,这个看起来很空洞。我无法解析它,user.institutions.length返回“0”。但是,当我查看Ember Inspector时,我可以在“数据”中看到该机构,因此该请求似乎正常运行......

我在模型上设置async:true。

以下是代码摘录:

  • 模特用户 - >

    export default DS.Model.extend({
         name: DS.attr('string'),
         surname: DS.attr('string'),
         email: DS.attr('string'),
         photo: DS.attr('string'),
         institutions: DS.hasMany('institution', {async: true}),
         fullName: Ember.computed("surname", "name", function fullName(){
           return this.get('surname') + " " + this.get('name');
         }),
         avatar: Ember.computed("photo", function avatar(){
           return "/images/avatars/" + this.get('photo');
         })
    });
    

机构:

export default DS.Model.extend({
  "shortname": DS.attr('string'),
  "photo": DS.attr('string'),
  "address": DS.attr('string'),
  "zipcode": DS.attr('string'),
  "city": DS.attr('string'),
  "type": DS.attr('string'),
  "dataset": DS.attr('string'),
  "website": DS.attr('string'),
  "phone" : DS.attr('string'),
  "rectorat": DS.attr('string'),
  "users": DS.hasMany('user', {async: true}),
  picture: Ember.computed("photo", function picture(){
    return "images/institutions/" + this.get('photo');
  })
});

服务器返回的JSON API:

{
   "links": {
      "self": "users/"
   },
   "data": {
      "type": "user",
      "id": 1,
      "attributes": {
         "user_id": 1,
         "name": "User",
         "surname": "Beta",
         "email": "test@xxx.com",
         "photo": "user.jpg",
         "institutions": [
            2
         ]
      },
      "links": {
         "self": "users//1"
      },
      "relationships": {
         "institutions": [
            {
               "links": {
                  "self": "institutions/2"
               },
               "data": {
                  "type": "institutions",
                  "id": 2,
                  "attributes": {
                     "institution_id": 2,
                     "shortname": "Standford Maternelle",
                     "photo": "mrclpgnl.jpeg",
                     "address": "12 rue Riquet",
                     "zipcode": "75019",
                     "city": "Paris",
                     "type": "Lycée",
                     "dataset": "xjdfhdjhkfshfdsjkhfdskjhf",
                     "website": "http://standford.com",
                     "phone": "0478585527",
                     "rectorat": "666881548451354564",
                     "users": [
                        1
                     ]
                  }
               }
            }
         ]
      }
   },
   "included": [
      {
         "type": "institutions",
         "id": 2,
         "attributes": {
            "institution_id": 2,
            "shortname": "Standford Maternelle",
            "photo": "mrclpgnl.jpeg",
            "address": "12 rue Riquet",
            "zipcode": "75019",
            "city": "Paris",
            "type": "Lycée",
            "dataset": "xjdfhdjhkfshfdsjkhfdskjhf",
            "website": "http://standford.com",
            "phone": "0478585527",
            "rectorat": "666881548451354564",
            "users": [
               1
            ]
         },
         "links": {
            "self": "institutions/2"
         }
      }
   ]
}

你是否知道为什么我没有正确的关系?我需要做些什么来做这样的事情:

{{#each user.institutions as |institution|}}
   {{institution.shortname}}
{{/each}} 

非常感谢!! :D

1 个答案:

答案 0 :(得分:0)

请尝试使用此功能:

{
    "data": {
      "type": "users",
      "id": "1",
      "attributes":{ 
        "name": "User",
        "surname": "Beta",
        "email": "test@xxx.com",
        "photo": "user.jpg",
      },
      "relationships": {
        "institutions": {
          "links": {
            "self": "/users/1/relationships/institutions",
            "related": "/users/1/institutions"
          },
          "data": [{ "type": "institutions", "id": "1" }]
        }
      }
    },
    "included": [{
      "type": "institution",
      "id": "1",
      "attributes": {
        "shortname": "Standford Maternelle",
        "photo": "mrclpgnl.jpeg",
        "address": "12 rue Riquet",
        "zipcode": "75019",
        "city": "Paris",
        "type": "Lycée",
        "dataset": "xjdfhdjhkfshfdsjkhfdskjhf",
        "website": "http://standford.com",
        "phone": "0478585527",
        "rectorat": "666881548451354564",
      },
      "links": {
        "self": "/institutions/1"
      }
    }]

  }

演示:http://jsbin.com/sekuji/edit?html,js,output