Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find record from belongsTo association in Ember.js

How can I get the associated record from an Ember model? Or: how to get the record from the Promise Object?

Customer model

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

Project model

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

Find method

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

Console output

<DS.PromiseObject:ember654> 

JSON response

{"projects":[
  {
    "id":1,
    "name":"test",
    "number":"a310",
    "description":null,
    "archived":false,
    "customer_id":22
  }
]};
like image 947
Slevin Avatar asked Dec 10 '13 19:12

Slevin


1 Answers

use another then on the get :)

var project = this.store.find('project', id).then(function(data) {
  data.get('customer').then(function(c){
    console.log(c);
  }
});
like image 120
Kingpin2k Avatar answered Sep 19 '22 04:09

Kingpin2k