Ember-Data的奇怪行为

时间:2014-06-30 18:57:17

标签: ember.js ember-data

我在使用Ember(v1.5.1)和Ember-Data(1.0.0-beta.8.2a68c63a)的应用程序中看到了一些奇怪的行为

我有一个简单的数据结构,其中联系人属于组织,并且都有许多组(异步)。这是(使用Ember-App-Kit):

联系模式:

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

});

组织模式:

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

我的API的结果是:

组织响应:

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

联系回复:

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

现在,当我尝试从联系人中检索组时,一切正常。但是,尝试通过联系人检索组织的组,它什么都不做,我甚至不在控制台中看到网络请求。更令人惊讶的是,如果我尝试直接从商店检索另一个组织并访问组,那就可以了。所以这让我觉得问题不在关系定义中,而是在其他地方。有什么想法吗?

内部路线 setupController:function(controller,model){     //调用_super以获取默认行为     this._super(控制器,型号);

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

更新:我知道联系人和组织之间的关系正常,因为我可以从用户访问该组织

1 个答案:

答案 0 :(得分:1)

您没有在联系人json中定义组织。

更新

您已将organization定义为非异步(默认情况下,无需明确调出)但您还没有在json中定义组织,只是ID。

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