jasalguero
jasalguero

Reputation: 4181

Strange behaviour with Ember-Data

I'm seeing some strange behaviour with my application using Ember(v1.5.1) and Ember-Data(1.0.0-beta.8.2a68c63a)

I have a simple data structure where Contacts belong to Organizations, and both have many Groups (async). Here is (using Ember-App-Kit):

CONTACT MODEL:

export default DS.Model.extend({

 organization: DS.belongsTo('organization', {async: false}),
 name: DS.attr('string'),
 lastname: DS.attr('string'),
 emails: DS.hasMany('email', {'async': true}),
 groups: DS.hasMany('group', {'async': true}),

 //... more internal functions

});

ORGANIZATION MODEL:

export default DS.Model.extend({

 name: DS.attr('string'),
 groups: DS.hasMany('group', {'async': true}),

 //... more internal functions

});

GROUP MODEL:

export default DS.Model.extend({
 name: DS.attr('string'),

 //... more internal functions
});

And the result from my API is:

ORGANIZATION RESPONSE:

{
  organization: {
  id: 3,
  name: "Organization X",
  links: {
   groups: "./groups"
  }
 }
}

CONTACT RESPONSE:

{
  contact: {
  id: 2,
  organization: 3,
  name: "John",
  lastname: "Smith",
  links: {
   emails: "./emails",
   groups: "./groups"
  }
 }
}

Now when I try to retrieve the groups from the Contact, everything works fine. But trying to retrieve the groups of an organization through a Contact it does nothing, I don't even see the network request in the console. More surprisingly is that if I try to retrieve another Organization from the store directly and access to the groups, that works fine. So that makes me think that the problem is not in the relationship definition, but somewhere else. Any ideas?

INSIDE ROUTE setupController: function(controller, model) { // Call _super for default behaviour this._super(controller, model);

var toLoad = [
  model.get('groups'), <--- THIS WORKS FINE
  model.get('organization').get('groups'), <---- THIS RETURNS EMPTY ARRAY, WITHOUT NETWORK REQUEST
  this.store.find('organization', 3) 
];

return Em.RSVP.all(toLoad, 'loading dependencies')
  .then(function(results) {
    return results[2].get('groups');
  }).then(function(groups){
    console.log(groups.get('length'));
    controller.set('orgGroups', groups); <---- THIS WORKS FINE TOO!!!!
  });
},

Updated: I know that the relationship between contact and organization is working because I can access the organization from the user

Upvotes: 2

Views: 68

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

You don't have organization defined in your contact json.

Update:

You've defined organization as non async (which it is by default, no need to explicitly call it out) yet you don't have the organization defined in the json returned, just the id.

{
  contact: {
  id: 2,
  organization: 3,
  name: "John",
  lastname: "Smith",
  links: {
   emails: "./emails",
   groups: "./groups"
  }
 },
  organizations:[
    {
      id: 'foo',
      name: 'bar'
    }
  ]
}

Upvotes: 1

Related Questions