Ember Router - determine if route/resource is child of current route

832 views Asked by At

Is there a way I can determine if a given route is the child of the current route?

I'm using the willTransition hook in a Route to determine whether I should display a data-loss confirmation message when the user transitions away from this route. I don't want this data-loss confirmation to execute if the user transitions between various child resources and routes contained within this resource's router map, but I cannot find a way to determine if the transition I'm given in willTransition is a child of this.routeName.

Any suggestions?

Router:

Router.map(function() {
   this.route('some-route')
   this.resource('parent-resource', function() {
     this.route('child-route');
     this.resource('child-resource', function() {
       this.route('child-resource-route1');
       this.route('child-resource-route2');
     }
   }
});

ParentResourceRoute:

Ember.Route.extend({
   actions: {
      willTransition: function(transition) {
        // if transition is not a child of this.routeName display a confirmation alert if there is dirty data 
      }
   }
});
1

There are 1 answers

2
Asgaroth On

How about

Ember.Route.extend({
 actions: {
  willTransition: function(transition) {
    if (transition.get('targetName').indexOf(this.get('routeName')) === false) {
      // moving to a sibling or higger route
    }
  }
 }
});

May need adjustments according to your route names.