2

検索バー付きの Ember アプリがあります。URLが変わるたびに検索バーの内容をクリアしたい。実際のアクションは簡単です (検索バーを簡単に取得する関数を作成しました)。

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

問題は、このコードをどこに置くかです。1 つの (むしろ魅力的ではない) オプションは、すべてのルートにそれを含めることです。ただし、より良いアプローチがあるはずだと思います。何かが組み込まれているのかもしれません。Em.Router にモンキーパッチを適用する方法があるのか​​もしれません。どんな提案でも大歓迎です。

4

1 に答える 1

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')

});
于 2013-11-15T05:49:07.647 に答える