【问题标题】:Ember cli - Models composed of multiple words and relationships (ember-data)Ember cli - 由多个单词和关系组成的模型(ember-data)
【发布时间】:2014-09-19 14:21:45
【问题描述】:

目前正在使用 Ember-cli 开发应用程序,但在使用具有 2 个单词和关系的模型时遇到了一些困难。

模范居民个人资料

//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),
});

居民路线上的模型挂钩:

//routes/residents.js

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

当我尝试仅获取居民个人资料时,我收到一个错误“residents.index Cannot read property 'typeKey'”

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

我正在使用 RESTAdapter 和一个 Restful API,

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

GET /residentsProfile { “居民档案”:[{ “身份证”:20, “图片”:空, “电话”:空, "firstName": "洛基", "lastName": "巴尔博亚", “blockId”:空, “单位标识”:空, "createdAt": "2014-09-17 19:54:28", "updatedAt": "2014-09-17 19:54:28", “居民账户”:[5] }] }

GET /residentsAccount { “居民账户”:[{ “身份证”:5, “电子邮件”:“rocky@balboainc.me”, “管理员”:假, “居民”:是的, "profileId": 20, "createdAt": "2014-09-17 19:54:29", "updatedAt": "2014-09-17 19:54:29", “居民简介”:[20] }] }

编辑

除了@Kingpin2k 提出的更改,这些更改是正确的,我使用 setupcontroller 的方式如下:

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

现在一切正常。

【问题讨论】:

    标签: ember.js ember-data ember-cli


    【解决方案1】:

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

    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 在那个范围内可能是窗口,这意味着你很可能传入 undefined。

    【讨论】:

    • 你说得对,我只是粘贴了我在编辑器中的所有内容,因为我(拼命地)尝试调试。所以这绝对是一个错误。我正在等待后端人员修复 api 以对其进行测试,然后将您的答案标记为正确。非常感谢您的回复!
    猜你喜欢
    • 1970-01-01
    • 2013-03-15
    • 2015-10-09
    • 2015-02-14
    • 1970-01-01
    • 2014-12-10
    • 2014-11-26
    • 1970-01-01
    • 2013-10-12
    相关资源
    最近更新 更多