2016-01-24 40 views
1

在Ember中是否可以加載多個模型,但不等待promise來解決並仍然使用afterModel鉤子?目前,我的代碼如下所示:Ember:在不等待的情況下加載模型

export default Ember.Route.extend({ 
    model() { 
    return Ember.RSVP.hash({ 
     imports: this.store.findAll('import', { 
     skip: 0, 
     limit: 5 
     }), 

     latestimport: this.store.find('import', 'latest') 
    }); 
    }, 

    afterModel(model) { 
    ...some modifications here... 
    } 
} 

由於使用Ember.RSVP的,頁面加載等待所有承諾,以解決它呈現前。我可以在模型加載之前使頁面呈現嗎?那麼我可以繼續使用afterModel鉤子嗎?我的觀點是,所加載的數據只是輔助信息。我不希望整個渲染過程因此被阻塞。我沒有在文檔中找到任何信息,儘管我感覺文檔並不完整。

回答

4

請使用以下方法:

setupController(controller, model) { 
    this._super(controller, model); 

    Ember.RSVP.hash({ 
    imports: this.store.findAll('import', { 
     skip: 0, 
     limit: 5 
    }), 

    latestimport: this.store.find('import', 'latest') 
    }).then(results => { 
    // ...some modifications here... 
    controller.set('model', results); 
    }); 
} 
  1. 它不應該阻止渲染。
  2. 您仍然可以對數據應用修改。
  3. 您仍然可以訪問模板中的數據爲model.imports,model.latestimport。的
1

,而不是包裝你的代碼Ember.RSVP.hash只是通過哈希對象,因爲它是

model() { 
    return { 
     imports: this.store.findAll('import', { 
     skip: 0, 
     limit: 5 
     }), 

     latestimport: this.store.find('import', 'latest') 
    }) 
    }, 

setupController(controller, model) { 
    this._super(controller, model); 
    // model.latestimport.then(...) 
    // model.imports.then(...) 
} 
相關問題