一起请求两个模型 [英] Request Two Models together

查看:141
本文介绍了一起请求两个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个很多很多的模型。它们被用在我的应用程序的第一页上,我无法加载它们。



两个模型只有少数项目(< 200)和我'只想将两个模型完全加载到一个 findAll 请求中。但是当第一个模型被加载时,Ember会逐个地逐个获取第二个模型的丢失数据。如果我试图单独加载模型,我会收到一个错误,必须为 hasMany {async:true} c> attr由于某些原因,Ember并没有认出第二个模型的要求。



有没有办法获取这两个模型,等待两个加载之前继续?



谢谢。

解决方案

我猜你在做一些沿着以下行:

  App.IndexRoute = Ember.Route.extend({
model:function() {
//获取第一个模型的记录
return this.store.find('post');
},

setupController:function(controller,模型){
this._super(controller,model);
this.store.find('comment')。then(function(comments){
controller.set('comments'评论)
});
}
});

模型返回的任何承诺该路由将导致路由器暂停转换,直到达成该承诺。在上述情况下,路由器将仅等待帖子请求解析。因此,我们需要指示路由器等待两个请求完成。



输入 Ember.RSVP.all Ember.RSVP.hash 。这些方法允许将多个承诺合并为一个。他们只有在履行所有的个人承诺的时候才能实现新的承诺。这是您如何使用 Ember.RSVP.hash

  App.IndexRoute = Ember.Route.extend({
model:function(){
var store = this.store;
return Ember.RSVP.hash({
posts: store.find('post'),
评论:store.find('comment')
});
},

setupController:function(controller,模型){
var posts = models.posts;
var comments = models.comments;

controller.set('content',posts);
控制器。 set('comments',comments);
}
});


I have two models that are many to many. They're used on the first page of my app and I'm having trouble loading them.

Both models only have a handful of items (<200) and I'd like to just load both models completely in one findAll request each. But as the first model gets loaded, Ember starts fetching the missing data for the second model, item by item. If I try to just load the models separately, I get an error and have to set {async:true} for the hasMany attr. For some reason though, Ember isn't recognizing the json of the requests for the second model.

Is there anyway to fetch both models and wait till the both load before continuing?

Thanks.

解决方案

I'm guessing you are doing something along the lines of:

App.IndexRoute = Ember.Route.extend({
  model: function() {
    // Fetch the records of the first model 
    return this.store.find('post');
  },

  setupController: function(controller, model) {
    this._super(controller, model);
    this.store.find('comment').then(function(comments) {
      controller.set('comments', comments)
    });
  }
});

Any promise returned from the model hook of the route, will cause the router to pause transitioning until that promise is fulfilled. In the above case, the router will wait only for the posts request to resolve. Therefore we need to instruct the router to wait for both requests to complete.

Enter Ember.RSVP.all and Ember.RSVP.hash. These methods allow for merging multiple promises into one. They return a new promise that is fulfilled only when all of the individual promises are fulfilled. Here is how you do it with Ember.RSVP.hash:

App.IndexRoute = Ember.Route.extend({
  model: function() {
    var store = this.store;
    return Ember.RSVP.hash({
      posts: store.find('post'),
      comments: store.find('comment')
    });
  },

  setupController: function(controller, models) {
    var posts = models.posts;
    var comments = models.comments;

    controller.set('content', posts);
    controller.set('comments', comments);
  }
});

这篇关于一起请求两个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆