简体   繁体   English

强制在Ember Cli中获取RSVP哈希中的所有数据

[英]Force to get all data in RSVP hash in Ember Cli

Ember Cli doesn't work with relational data (relatesTo and hasMany) the way Ember normally should do. Ember Cli不像Ember通常应该做的那样使用关系数据(relateTo和hasMany)。 In order to make it work, I use a RSVP hash and set the model in the controller manually. 为了使其工作,我使用RSVP哈希并手动在控制器中设置模型。

export default Ember.Route.extend({

  model: function(params) {
    return Em.RSVP.hash({
      traits: this.store.find('trait'),
      person: this.store.find('person', params.person_id)
    });
  },

  setupController: function(controller, model) {
    controller.set('model', model.person);
  }
});

This gets the person with the given ID and his traits. 这使得具有给定ID和他的特征的人。 In this case, since the person only has two traits assigned, only those two data objects are returned. 在这种情况下,由于人员只分配了两个特征,因此只返回那两个数据对象。

Let's say on this route I want to give the user the ability to change the traits of this person. 让我们说在这条路线上,我想让用户能够改变这个人的特征。 So, I want to display ALL traits with checkboxes. 所以,我想用复选框显示所有特征。 How in the route above do I force the request to get all the traits, not just two? 如何在上面的路线中强制要求获得所有特征,而不仅仅是两个?

I tried store.all(), but that only filters the local data. 我尝试了store.all(),但只过滤了本地数据。 Since no data is present yet, it return 0 objects. 由于还没有数据,它返回0个对象。

Ember-cli just helps with setting up your project structure and building it. Ember-cli只是帮助您设置项目结构并构建它。 It doesn't run Ember any different. 它没有任何不同的Ember运行。 If you want all of the traits in your template or on your controller you should actually keep track of them after you've fetched them. 如果你想要模板或控制器上的所有特征,你应该在获取它们之后实际跟踪它们。

export default Ember.Route.extend({

  model: function(params) {
    return Em.RSVP.hash({
      traits: this.store.find('trait'),
      person: this.store.find('person', params.person_id)
    });
  },

  setupController: function(controller, model) {
    controller.set('model', model.person);
    controller.set('allTraits', model.traits);
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM