2014-07-21 16 views
0

我希望多個變量切換爲真或假,具體取決於控制器在頁面加載時檢查的當前路線。我希望多個變量切換爲真或假,具體取決於當前路線

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

回答

1

確實有兩組主要路徑變化或「transitions」出現在您的應用中。

第一個: Ember將初始化,路由器將把用戶轉換到所需的上下文。如果您有嵌套的資源,這可能需要幾次轉換。如果用戶返回到嵌套的資源或重新加載頁面,Ember很聰明,並且會用上下文重新加載。因此,自動轉換。

第二個:用戶可以根據您製作的任何用戶界面切換到應用程序。

要解決兩個問題中的第一個問題,您可能需要初始化每個路由的控制器,以使用setupController掛鉤來激活currentPath屬性。這種方式在頁面加載路由將執行第一個setter,然後觀察者將處理任何基於用戶的路徑更改。

這是您可以放置​​在應用程序控制器中的通用路徑觀察器。 這將解決兩個問題第二:

App.ApplicationController = Ember.Controller.extend({ 
    needs: ['super-table', 'users', 'locations'], 
    currentPathDidChange: function() { 
    var path_base = this.get('currentPath').split('.')[0]; 
    this.set('controllers.super-table.currentPath', path_base); // give your super table template the path 
    this.nullifyActiveFlags(); // You can write this yourself... 
    switch(path_base){ // just give me the base resource 
     case 'users': 
     this.set('controllers.users.activePath', true); 
     break; 
     case 'locations': 
     this.set('controllers.locations.activePath', true); 
     break; 
    } 
    }.observes('currentPath'), 
//... 

我可能會拉該回調邏輯從觀察者的和到其自己的方法,但是這僅僅是僞代碼顯示什麼是可能的。

您還應該重構超級表currentPath setter作爲超級表控制器內的計算別名。

讓我知道是否有任何額外的解釋需要!我使用過類似的模式很多次,並且配置正確時它工作得非常好。

+0

'locations'和'users'都使用超級表控制器,那麼將此答案中的代碼添加到該控制器而不是應用程序控制器中是否合理?如果是這樣的話,那麼我不需要在超級桌面上調用'需求',對吧? –

+1

沒有。 'currentPath'是一個應用程序控制器屬性。 –

+1

你可以從任何地方調用'controllers.application.currentPath' ...所以我想你可以把這個觀察者放在任何你想要的地方。聽起來像這樣很難保持乾爽。 –

1

要直接回答你的問題,undefined在Javascript中是falsey的,所以你實際上不需要明確地將它設置爲false。如果您想要通用函數將當前路徑設置爲true,只需使用正則表達式即可。例如:

setCurrentPath: function() { 
    this.set(("routed_to_" + currentPath.match(/\/(.*)/)[1]).camelize(), true); 
} 

作爲巨大警告,我還將添加你可能不應該在第一時間做的任何現象。一般來說,如果你發現自己寫

if (isAThing) { 
    ... 
} else if (isAnotherThing) { 
    ... 
} else if (isYetAnotherThing) { 
    ... 
} 

你可能會發現command patternchain of responsibility pattern有用。另外,在Handlebars中放置大量的邏輯會讓你的表現吃光,因爲每當有所改變時,它都必須操縱你的DOM。如果您需要邏輯,請將其保留在Javascript中,並儘量減少模板中的計算屬性。如果這更復雜,讓路由器決定將什麼東西吐出來。

在文字中傳達語氣非常困難,我希望我不會像我一樣毆打你。只是想傳授一些辛苦的教訓。祝你好運。

+0

謝謝特拉維斯。這個答案也有效!是的,我曾嘗試爲我的應用程序中使用列表的任何東西製作這個超級靈活的模板,並且它使用了句柄的TON #if條件,根據當前的數據創建和銷燬此模板的每個方面。這有嚴重的性能問題。 –