Generic behavior

I want to add generic behaviors to my app. If I call back(), application should go one step up in the route hierarchy. posts/3/edit → posts.post

  this.route('posts', function() {
    this.route('post', { path: "/:post_id" }, function() {
      this.route('edit');
    });
  });

My first question: where should I put the code? Is the controller responsible for handling this, or the route? It feels for me, it should go with the route.

editRoute

actions:{
   back: function(){
        //manage back
    }
}

I’m lazy I want to keep it DRY as possible, and i don’t want to add this to every route. and I don’t want to make a route base class, import it to each route. What about reopen Ember.Route? Is it the way to do it? I tried the following, but I’m getting

Uncaught Error: Nothing handled the action ‘back’.

import Ember from "ember";
export function initialize(/* container, application */) {
  // application.inject('route', 'foo', 'service:foo');
    console.log("init");
    Ember.Route.reopen({
        actions:{
            back:function(){
                console.log("back");
            }
        }
      
  })
};

export default {
  name: 'route',
  initialize: initialize
};

Define it in the application route as an action, then use action bubbling to trigger it.

@varblob thank you for the quick reply. First of all, why is my approach (reopening) does not work? If the user click my button component it send the action back() to the controller. In my controller there is no action, so (i guess) ember looks into the posts.post.edit route. Since it inherits from Ember.Rout is should have a back() action it in. Maybe i am missing something.

You suggested, that application route. I understand, that the action will bubble up from

controller posts.post.edit →

route. posts.post.edit →

route. posts.pos →

route. posts →

route application (every road leads to Rome application route)

But how does application rout know the current app route ? Is there something like

this.get('current.route').parent()

I should have added this caveat in my first response. I’m pretty weary of reopening ember objects. I’ve found that it often doesn’t work the way I expected even with seemingly simple adjustments. This could be because I’m just not knowledgable enough but that’s been my experience thus far.

One way you could implement this in the application route is by keeping track of the transition history. I don’t believe ember keeps track of this for you.

I haven’t played too much with transitions but I think I recall seeing on the transition object a non public bit of api that’s related to the resource stack you maybe able to use at your own risk.