在Ember.js中查找belongsTo协会的记录

时间:2013-12-10 19:47:02

标签: ember.js ember-data

如何从Ember模型中获取相关记录?或者:如何从Promise对象中获取记录?

客户模式

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
  }
]};

1 个答案:

答案 0 :(得分:11)

使用另一个然后在get:)

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