首页 文章

Ember cli - 由多个单词和关系组成的模型(余烬数据)

提问于
浏览
1

目前正在开发一个使用Ember-cli的应用程序,并且对于具有2个单词和关系的模型有一些困难 .

Model residents-profile

//models/residents-profile.js
DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    picture: DS.attr('string'),
    phone: DS.attr('string'),
    gender: DS.attr('string'),
    residentsAccount: DS.belongsTo('residentsAccount')
}

模特居民 - 帐户

//models/residents-account.js
DS.Model.extend({
    email: DS.attr('string'),
    password: DS.attr('string'),
    profileId: DS.attr('number'),
    residentsProfile: DS.belongsTo('residentsProfile', this.profileId),
});

the model hook on the residents route:

//routes/residents.js

    model: function() {
            return Ember.RSVP.hash({
              residentsProfile: this.store.find('residentsProfile'),
              residentsAccount: this.store.find('residentsAccount')
            })
    }

只要我尝试获取居民资料,我就会收到错误“resident.index无法读取属性'typeKey'”

但是,如果我从居民资料中删除关系密钥并仅调用居民资料,则可以正确获取数据 .

我正在使用RESTAdapter和Restful API,

模型将单独返回,服务器的响应如下:

GET /residentsProfile {"residentsProfiles":[{"id":20,"picture":null,"phone":null,"firstName":"Rocky","lastName":"Balboa","blockId":null,"unitId":null,"createdAt":"2014-09-17 19:54:28","updatedAt":"2014-09-17 19:54:28","residentsAccount":[5]}]}

GET /residentsAccount {"residentsAccounts":[{"id":5,"email":"rocky@balboainc.me","admin":false,"resident":true,"profileId":20,"createdAt":"2014-09-17 19:54:29","updatedAt":"2014-09-17 19:54:29","residentsProfile":[20]}]}

EDIT

Besides the changes proposed by @Kingpin2k, which were spot on, i used setupcontroller in the following way:

setupController: function(controller, 
        controller.set('residentsAccount', models.residentsAccount);
        controller.set('residentsProfile', models.residentsProfile);
    }

现在一切正常 .

1 回答

  • 2

    三个问题,你们的关系都应该是异步的(因为它们不会在同一个响应中返回)

    residentsAccount: DS.belongsTo('residentsAccount', {async: true})
    
    residentsProfile: DS.belongsTo('residentsProfile', {async:true})
    

    并且来自它们的两个json响应应该是单个id,而不是数组

    {  
       "residentsProfiles":[  
         {  
         "id":20,
         "picture":null,
         "phone":null,
         "firstName":"Rocky",
         "lastName":"Balboa",
         "blockId":null,
         "unitId":null,
         "createdAt":"2014-09-17 19:54:28",
         "updatedAt":"2014-09-17 19:54:28",
         "residentsAccount":  5
         }
       ]
    }
    

    最后,我在这里试图用 this.profileId 完成,但它可能没有按照你的想法做到 . 该范围内的 this 可能是窗口,这意味着您可能会传递未定义的内容 .

相关问题