在Ember JS中按顺序加载多个模型的最佳方法是什么?
e.g。
App.ProductRoute = Ember.Route.extend({
model: function(params) {
// first call
this.store.find('Product');
// second call should only be called after the first find is completed
var prod = this.store.find('Product', params.product_id);
// third call should only be called after the above is completed
this.store.find('ProductOptions', prod.get('optionId');
return ???
},
setupController: function(controller, model){
// how would one the set up the controllers?
}
});
This帖子展示了如何同时在路线中加载多个模型。
答案 0 :(得分:3)
您可以创建自己的承诺,并解决哈希问题。这将是控制器中的模型,无需覆盖setupController
,除非您希望它们存储在控制器的某个不同位置。
App.ProductRoute = Ember.Route.extend({
model: function(params) {
var self = this;
return new Em.RSVP.Promise(function(resolve, reject){
// first call
self.store.find('Product').then(function(products){
// second call should only be called after the first find is completed
self.store.find('Product', params.product_id).then(function(product){
// third call should only be called after the above is completed
self.store.find('ProductOptions', product.get('optionId').then(function(productOption){
resolve({
products:products,
product:product,
productOptions:productOptions
});
});
});
});
});
}
});
BTW,听起来产品选项应该是产品的异步关系。