Search code examples
javascriptember.jscross-browserurl-routingdetect

Is there a way to detect route changes with a resource?


I am utilizing an ember resources as part of my application routing. I use deactivate on most of my routes to detect when it is changing, but can't seem to get it to work in my controller or view files. I have only seen success with deactivate when placing it in my route files. Is there a better way to go about this?

My main problem is that I need to detect when the page or route changes after another button and subsequent function have been called. The user process is as follows:

  1. User navigates to page (which is a resource route)
  2. User clicks a button to activate a feature
  3. User navigates away from current page Need to detect that change and hide the activated feature

Solution

  • You can have an active property on the controller that your button is bound to.

    //yourcontroller.js
    App.Yourcontroller = Ember.ObjectController.extend({
      active: true
    });
    
    // template
    {{#if active}}
      <button>
    {{/if}}
    

    And change that value on the route's deactivate

    // route.js
    deactivate: function() {
      this.controllerFor('yourcontroller').set('active', false);
    }