2013-12-10 29 views
7

如何從Ember模型獲取關聯記錄?或者:如何從Promise Object獲取記錄?在Ember.js中查找belongsTo協會的記錄

客戶模式

Docket.Customer = DS.Model.extend({ 
    name:  DS.attr('string'), 
    initial:  DS.attr('string'), 
    description: DS.attr('string'), 
    number:  DS.attr('string'), 
    archived: DS.attr('boolean'), 
    projects: DS.hasMany('project',{ async: true }) 
}); 

項目模型

Docket.Project = DS.Model.extend({ 
    name:  DS.attr('string'), 
    description: DS.attr('string'), 
    number:  DS.attr('string'), 
    archived: DS.attr('boolean'), 
    customer: DS.belongsTo('customer', { async: true }) 
}); 

查找方法

var project = this.store.find('project', id).then(function(data) { 
    console.log(data.get('customer').toString()); 
}); 

控制檯輸出

<DS.PromiseObject:ember654> 

JSON響應

{"projects":[ 
    { 
    "id":1, 
    "name":"test", 
    "number":"a310", 
    "description":null, 
    "archived":false, 
    "customer_id":22 
    } 
]}; 

回答

10

使用另一個則在得到:)

var project = this.store.find('project', id).then(function(data) { 
    data.get('customer').then(function(c){ 
    console.log(c); 
    } 
}); 
+1

哦該死!捂臉。謝謝! – Slevin