2014-01-19 32 views
2

在Ember JS中依次加载多个模型的最佳方式是什么?如何在Ember JS路线中顺序加载多个模型

例如

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后显示了如何在一个路径的同时加载多个模型。

回答

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 
       }); 
      }); 
     }); 
     }); 
    }); 
    } 
}); 

顺便说一句,这听起来像产品选项应该是产品上的异步关系。

+0

谢谢kingpin2k - 你太棒了! – TrevTheDev

相关问题