如何在Ember JS路由中依次加载多个模型 [英] How to load multiple models sequentially in Ember JS route

查看:85
本文介绍了如何在Ember JS路由中依次加载多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,

  App.ProductRoute = Ember.Route.extend({
model:function(params){
// first call
this.store.find('Product' );
//第二次调用只能在第一次查找完成后调用
var prod = this.store.find('Product',params.product_id);
//第三次调用只有在上述完成后才可以调用
this.store.find('ProductOptions',prod.get('optionId');
return ???
},
setupController:function(controller,model){
//如何设置控制器?
}
});

post显示如何同时在路由中加载多个模型。

解决方案

您可以创建你自己的承诺,并解决哈希。这将是控制器中的模型,不需要覆盖 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){

//第二次调用只能在第一次查找完成后调用
self.store.find('Product',params.product_id).then(function(product){
//第三次调用只应该在上面完成后调用
self.store.find('ProductOptions',product.get('optionId')然后(function(productOption){
resolve({
products:产品,
产品:产品,
productOptions:productOptions
});
});
});
});
});
}
});

BTW,听起来像产品选项应该是产品上的异步关系。


What is the best way to load multiple models sequentially in 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 post shows how to load multiple models in a route at the same time.

解决方案

You can create your own promise, and resolve a hash. That will be the model in the controller, no need to override setupController unless you want them stored on the controller somewhere different.

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, it sounds like product options should be an async relationship on the product.

这篇关于如何在Ember JS路由中依次加载多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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