一起请求两个模型 [英] Request Two Models together
问题描述
两个模型只有少数项目(< 200)和我'只想将两个模型完全加载到一个 findAll
请求中。但是当第一个模型被加载时,Ember会逐个地逐个获取第二个模型的丢失数据。如果我试图单独加载模型,我会收到一个错误,必须为 hasMany $ c $设置
{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屋!