2015-03-25 78 views
0

我对烬来说很新,但我想知道这是否是我的逻辑问题,或者是否有某些东西在烬中不能正常工作。Ember数据一对多关系不起作用

当我尝试model.get('profile')时,我收到空。

我已经能够获取轮廓模型,像这样: this.store.find('profile', 17); 我也可以在PROFILE_ID属性添加到选址模型,然后用它

var location = this.modelFor('location'); 
var profile = this.store.find('profile', 17); 

但是从我可以在文档中找到我应该可以在位置对象上使用​​来获取其配置文件。

奇怪的是我的附件关系(位置有很多附件)完美地工作。

我错过了什么吗?我做错了什么导致这种关系失败?

我有三个型号:

locatons->

export default DS.Model.extend({ 
    image: DS.attr('string'), 
    latitude:DS.attr('string'), 
    longitude:DS.attr('string'), 
    outlets:DS.attr('string'), 
    parking:DS.attr('string'), 
    internet:DS.attr('string'), 
    credit_cards:DS.attr('string'), 
    share_url:DS.attr('string'), 
    roaster:DS.attr('string'), 
    about:DS.attr('string'), 
    name: DS.attr('string'), 
    address: DS.attr('string'), 
    images: DS.attr('string'), 
    attachments: DS.hasMany('attachment'), 
    profile: DS.belongsTo('profile'), 
    curator_image: DS.attr('string'), 
    curator_about: DS.attr('string'), 
    curator_name: DS.attr('string'), 
    style_image_url: function(){ 
    return "background-image:url('" + this.get("image") + "')"; 
    }.property("image"), 
    style_profile_image_url: function(){ 
    return "background-image:url('" + this.get("curator_image") + "')"; 
    }.property("curator_image"), 
}); 

附件 - >

export default DS.Model.extend({ 
    location: DS.belongsTo('location'), 
    image:DS.attr('string') 
}); 

配置文件 - >

export default DS.Model.extend({ 
    location: DS.hasMany('location'), 
    name:DS.attr('string'), 
    about:DS.attr('string'), 
    image:DS.attr('string'), 
}); 

这里是从一个样本响应服务器: locations.json

{ 
    attachments: [ 
    { 
     id: 254, 
     image: "https://dripper-dev.s3-us-west-1.amazonaws.com/uploads/large_9443c014-69e1-4616-943d-e627a47f8306.jpg?AWSAccessKeyId=AKIAJPYWBHET4VVL5NUA&Signature=nD3YMiuFiHtpuTPop77Q%2BS6N9HM%3D&Expires=1427847353" 
    }, 
    { 
     id: 250, 
     image: "https://dripper-dev.s3-us-west-1.amazonaws.com/uploads/large_3e562933-5ce2-4f91-bb6f-6fe6883e0463.jpg?AWSAccessKeyId=AKIAJPYWBHET4VVL5NUA&Signature=ESbhuHNjx9kD63gJY3UVRsHs2B8%3D&Expires=1427847353" 
    }], 
    locations: [ 
    { 
     id: 12, 
     latitude: "45.550346", 
     longitude: "-122.666584", 
     address: "3808 North Williams Avenue, Portland, OR 97212, USA", 
     outlets_text: "Yes", 
     internet_text: "No", 
     roaster: "Ristretto Roasters", 
     parking_text: "Yes", 
     credit_cards: true, 
     image: "https://dripper-dev.s3-us-west-1.amazonaws.com/uploads/medium_9b676fc6-814c-4f5f-a03b-5a5f650fc7aa.jpg?AWSAccessKeyId=AKIAJPYWBHET4VVL5NUA&Signature=w5pSN5iJbtlWfiFo2UMcJMN6pAs%3D&Expires=1427847347", 
     name: "Ristretto", 
     twitter_username: null, 
     curator_image: "https://dripper-dev.s3-us-west-1.amazonaws.com/uploads/basic_uploader/Profile/9d77af52-033b-4bd9-8cb4-8b961f366392.jpg?AWSAccessKeyId=AKIAJPYWBHET4VVL5NUA&Signature=YsgxmH%2B5wmlOpVSoti5kn0DozKQ%3D&Expires=1427847347", 
     curator_name: "Jack White", 
     curator_about: "this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test ", 
     profile_id: 17, 
     attachment_ids: [ 
      30 
     ] 
    }, 
    ... 

路线: /routes/location.js

export default Ember.Route.extend({ 
    model: function(params) { 
     return this.store.find('location', params.location_id); 
    } 
}); 

/路由/位置/索引

export default Ember.Route.extend({ 
    model: function() { 
     return this.modelFor('location').get('profile'); 
    } 
}); 

更新: 我能够获取配置文件模型通过更改我的JSON到:

{ 
    attachments: [], 
    locations: [ 
    { 
     id: 12, 
     latitude: "45.550346", 
     longitude: "-122.666584", 
     address: "3808 North Williams Avenue, Portland, OR 97212, USA", 
     outlets_text: "Yes", 
     internet_text: "No", 
     roaster: "Ristretto Roasters", 
     parking_text: "Yes", 
     credit_cards: true, 
     image: "https://dripper-dev.s3-us-west-1.amazonaws.com/uploads/medium_9b676fc6-814c-4f5f-a03b-5a5f650fc7aa.jpg?AWSAccessKeyId=AKIAJPYWBHET4VVL5NUA&Signature=bhPpdV6bxroZZ0Lfk2jN6aPht7A%3D&Expires=1427916453", 
     name: "Ristretto", 
     twitter_username: null, 
     curator_image: "https://dripper-dev.s3-us-west-1.amazonaws.com/uploads/basic_uploader/Profile/9d77af52-033b-4bd9-8cb4-8b961f366392.jpg?AWSAccessKeyId=AKIAJPYWBHET4VVL5NUA&Signature=K1VuT3Oe8ncTdX%2FA9H7s4XJcQ84%3D&Expires=1427916453", 
     curator_name: "Jack White", 
     curator_about: "this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test ", 
     profile_id: 17, 
     profile: { 
      id: 17, 
      name: "Jack White", 
      about: "this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test ", 
      action_url: null, 
      action_caption: null, 
      published: null, 
      image: { 
       url: "https://dripper-dev.s3-us-west-1.amazonaws.com/uploads/basic_uploader/Profile/9d77af52-033b-4bd9-8cb4-8b961f366392.jpg?AWSAccessKeyId=AKIAJPYWBHET4VVL5NUA&Signature=E%2BgWHlMRcIXDeMOTmLhfdU8YyTc%3D&Expires=1427916459" 
      }, 
      user_id: 9, 
      created_at: "2015-03-24T20:07:31.043Z", 
      updated_at: "2015-03-24T20:09:01.518Z" 
     }, 
     attachment_ids: [ 
     30 
     ] 
    }, ... 

救了我的位置,对象我的路线window.loc,然后在浏览器控制台检查: loc.get('profile').get('name') 回报"Jack White"这是正确的,但我对到底为什么这种变化有差别不清楚。侧加载(放置相关对象的ID和包含这些对象的顶级数组)仅适用于烬数据中的一对多关系?

任何和所有的输入是受欢迎的,我仍然是新的烬数据。

+0

最小可重复性测试用例。帮助我们帮助你。 – givanse 2015-03-25 03:09:16

+0

你的location.json包含'cprofile_id:17'而不是'profile_id:17'。也许这只是一个打字错误,但也可能是问题所在。 – kunerd 2015-03-25 10:02:21

+0

我很抱歉,'cprofile'是json中的一个错字。在发布之前我尝试的最后一件事情之一是创建一个名为cprofile的新模型来测试配置文件是否为关键字。 – Jessiah 2015-03-25 19:04:12

回答

1

我发现解决方案在json中将profile_id: xx重命名为profile: xx。我认为model_id是注意归属关系的正确方法,但是实际上,ember只需要模型名称。希望这有助于未来的人!

0

我可能在这里很愚蠢,但您的位置模型没有配置文件字段 - 大概是因为它属于配置文件。但是,如果没有该字段,您将无法在位置上执行.get('profile')。

+0

我确实有位置模型中的配置文件字段:'profile:DS.belongsTo('profile')' - 我是否还需要profile_id字段? – Jessiah 2015-03-25 19:04:54

+0

啊,所以你呢,我正在愚蠢!你不应该需要profile_id。看看Kunerd说过的话,可能是sideloading已经做错了 – millerbr 2015-03-26 10:34:24

1

您的locations.json中没有侧面加载的配置文件对象,就像附件一样。它看起来更像是:

{ 
    locations: [{ 
    id: 12, 
    ... 
    profile_id: 17, 
    attachment_ids: [...] 
    }], 
    attachments: [...], 
    profiles: [...] /* for multiple profiles, or*/ 
    profile: {...}  /* for a single side-loaded model */ 
} 
... 

否则,如果要加载profile模型异步的,你需要在你location模型,其声明如下:

profile: DS.belongsTo('profile', { 
    async: true 
}