我希望多個變量切換爲真或假,具體取決於控制器在頁面加載時檢查的當前路線。我希望多個變量切換爲真或假,具體取決於當前路線
VpcYeoman.SuperTableController = Ember.ArrayController.extend({
routedToLocations: false,
routedToUsers: false,
currentPath: '/',
checkCurrentPath: function() {
if (currentPath == '/users')
this.set('routedToUsers', true)
} elsif (currentPath == '/locations') {
this.set('routedToLocations', true)
}
});
superTable.hbs
{{#if routedToUsers}}
Users!
{{/if}}
{{#if routedToLocations}}
Locations
{{/if}}
在users.hbs在locations.hbs
{{render superTable model}} //which will result in the string 'Users!'
{{render superTable model}} //which will result in the string 'Locations!'
或者,也許我可以添加變量,如routedToUsers
,在'用戶'控制器內的設置值。它會是什麼樣子,
- users_controller.js -
routedToUsers: true,
canBeEdited: false,
canBeDeleted: true,
這樣,每個超表有這些變量,除了他們已經預定義。另一個例子。
- locations_controller.js -
routedToUsers: false, // this is obviously false but for example's sake
routedToLocations: true,
canBeEdited: false,
canBeDeleted: false,
所以,如果我說我的路由到「用戶」的情況下,控制器將使用「checkCurrentPath
」,以確保我確實是在用戶頁面上的其他網頁上點擊一個#link-to
。
'locations'和'users'都使用超級表控制器,那麼將此答案中的代碼添加到該控制器而不是應用程序控制器中是否合理?如果是這樣的話,那麼我不需要在超級桌面上調用'需求',對吧? –
沒有。 'currentPath'是一個應用程序控制器屬性。 –
你可以從任何地方調用'controllers.application.currentPath' ...所以我想你可以把這個觀察者放在任何你想要的地方。聽起來像這樣很難保持乾爽。 –