2014-02-24 22 views
3

我需要設置控制器的多種型號的機型applicationRoute鉤時:Ember.js回報承諾在ApplicationRoute設置多個控制器型號

model: function() {  
    this.controllerFor('markets').set('model', this.store.pushPayload('market', marketsCache)); 
    this.controllerFor('pages').set('model', this.store.pushPayload('page', pagesCache)); 
    this.controllerFor('categories').set('model', this.store.pushPayload('category', categoriesCache)); 
    this.controllerFor('employees').set('model', this.store.pushPayload('employee', employeesCache)); 
} 

在我的網絡服務器index.php文件中我設置的JavaScript變量marketsCachepagesCache,categoriesCacheemployeesCache。它們從APC緩存中檢索,因爲API是查詢密集型的。如您所見,我希望應用程序等待模型滿員。但是,就我所知,承諾僅適用於AJAX請求。所以,問題是,是否有可能將這些controllerFor行列入承諾?

回答

2

pushPayload不返回一個承諾,也沒有一個模式可言,但你可以把有效載荷,然後調用all它將返回該類型的商店中的所有記錄,然後您可以將其分配給控制器。

model: function() {  
    this.store.pushPayload('market', marketsCache); 
    this.store.pushPayload('page', pagesCache) 
    .... 

    var market = this.store.all('market'), 
     pages = this.store.all('page'); 
    .... 

    this.controllerFor('markets').set('model', model.market); 
    this.controllerFor('pages').set('model', model.pages); 
    .... 
} 
+0

感謝您的評論!但是,當我做一個:'this.store.find' REST適配器被調用我想?那真的不是我想要的。因爲該API將獲得該模型的另一個調用。 – DelphiLynx

+0

對不起,我只是想過,我意識到你可能不想這樣做,你可以忽略散列和setupController,只是使用'全部' – Kingpin2k

+0

更新反映使用所有 – Kingpin2k

0

您可以用jQuery做到這一點遞延/時間:

var market = this.store.pushPayload('market', marketsCache); 
this.controllerFor('markets').set('model', market); 

var page = this.store.pushPayload('page', pagesCache); 
this.controllerFor('pages').set('model', page); 

var category = this.store.pushPayload('category', categoriesCache); 
this.controllerFor('categories').set('model', category); 

var employee = this.store.pushPayload('employee', employeesCache); 
this.controllerFor('employees').set('model', employee); 

return $.when(market, page, category, employee); 

文檔:https://api.jquery.com/jQuery.when/

+0

pushPayload不返回承諾。 – Kingpin2k

+0

@ kingpin2k確實,我編輯了我的代碼,'$ .when'沒有工作。我也根據你的答案在這裏創建了一個帶'RSVP.hash'的版本:http://stackoverflow.com/questions/19331835/request-two-models-together。但是,現在也工作,因爲pushPayload不會返回一個承諾。 – DelphiLynx