2016-05-29 73 views
0

密碼說千言萬語;有沒有辦法做我在這裏嘗試的?通過Ember JS RSVP哈希循環?

用戶 「根」 路線

在頂層的子路徑,如indexshoweditcreate等內使用裝載通用可選擇的選項...

-- /pods/users/route.js 

model() { 
    return RSVP.hash({ 
     userCategories: this.store.findAll('user-category'), 
     userSources: this.store.findAll('user-source'), 
     userGroups:  this.store.findAll('user-group'), 
    }); 
}, 

用戶「創建」路線

隨着子路線/create我要遍歷上面已經加載選項並將它們加載到模板命名變量:

-- /pods/users/create/route.js 

setupController(controller, model) { 

    let rootModel = this.modelFor('users'); 

    rootModel.forEach(function (model, name) { 
     set(controller, name, model); 
    }); <-- THIS IS THE BROKEN PART... 

    ... 

} 

的問題

.forEach環路我得到Error while processing route: rootModel.forEach is not a function TypeError: rootModel.forEach is not a function

是有一個循環遍歷根模型的「Ember方式」,或者我會被加載到頂層變量中,即:

-- /pods/users/create/route.js 

setupController(controller, model) { 

    let rootModel = this.modelFor('users'); 

    set(controller, 'rootModel', rootModel); 

    ... 

} 
+0

爲什麼你需要循環它?用你現在定義你的模型鉤子的方式,你可以用''{#each model.userCategories as | category |}}來訪問模板中的數據' – Igor

+0

我想這更像是一種可讀性偏好,我更願意能夠在沒有領先的'model'的情況下訪問'userCategories'。從邏輯上講,我知道這對任何一種方式都沒有任何影響,但對我來說'{{#each userCategories as | userCategory |}}'看起來更清晰。 – SamSebastian

回答

4
Object.keys(rootModel).forEach(function(name) { 
    var model = rootModel[keyName]; 
    set(controller, name, model); 
    // ... 
}); 

或可能你只需要不RSVP.hashRSVP.all其返回與數組包含解析值解決了一個承諾?