2013-11-15 93 views
2

我有一個帶搜索欄的Ember應用程序。每次URL更改時,我都想清除搜索欄的內容。實際操作很簡單(我創建了一個功能來輕鬆獲得搜索欄):每當URL更改時運行操作

App.searchBar().set('content', "") 

問題是,我在哪裏放這段代碼?其中一個(相當不吸引人)的選擇是將其包含在每條路線中。不過,我假設必須有更好的方法。也許有些東西是烘焙的。也許有一種方法可以用來修飾Em.Router。任何建議將不勝感激。

回答

3

您可以在currentPath上的應用程序控制器中添加一個觀察器,並在每次更改時都運行該觀察器。

App.ApplicationController = Em.Controller.extend({ 
    resetMySearchBar: function(){ 
    App.searchBar().set('content', ""); 
    }.observes('currentPath') 
}); 

而且,控制您的搜索欄的生活,你可以添加一個需求的應用控制器,並且當它觀察currentPath和更新上。

App.SearchBarController = Em.Controller.extend({ 
    needs: ['application'], 
    resetMySearchBar: function(){ 
    this.set('content', ""); // or wherever this property lives 
    }.observes('controllers.application.currentPath') 

}); 
+0

美麗。這比我預期的要簡單得多。 – nullnullnull