nullnullnull
nullnullnull

Reputation: 8189

Run action every time the URL changes

I have an Ember app with a search bar. I want to clear the contents of the search bar every time the URL changes. The actual action is easy (I've created a function to easily get the search bar):

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

The problem is, where do I put this code? One (rather unappealing) option would be to include it in every route. I'm assuming there must be a better approach, though. Maybe there's something baked in. Maybe there's a way to monkeypatch Em.Router. Any suggestions would be highly appreciated.

Upvotes: 2

Views: 232

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

You can add an observer in the application controller on currentPath, and every time it changes run that.

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

Additionally, on the controller where your search bar lives, you could add a needs for the application controller, and observe the currentPath and update when it does.

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

});

Upvotes: 3

Related Questions