1
votes

I want to monitor App.Router.router.currentState for the purpose of activating/deactivating the navigation links. Something along the lines as explained here: https://stackoverflow.com/a/13312466/674525

However, the property currentState does not seem to exist in the router any more. App.get('Router.router.currentState') returns undefined.

I suppose, it changed in the recent Ember versions. Is there another way of doing this?

4

4 Answers

11
votes

I believe the "textbook" way to do that using the new Ember builds will be:

App.SomeController = Ember.Controller.extend({
    needs: 'application',
    someFunction: function(){
        var state = this.get('controllers.application.currentPath');
    }
})
9
votes

This got kind of complicated with the current release for me, but at least it works:

var router = App.__container__.lookup("router:main"); //lookup the router
var currentHandlerInfos = r.router.currentHandlerInfos; //there are multiple handlers active at one time
var activeHandler = currentHandlerInfos[currentHandlerInfos.length - 1]; // the last item in the array is the current handler with properties like name, context, handler (Ember.Route)
var activeRoute = activeHandler.handler; //your route object

This code is inspired by reading this Ember Source. I have not yet tested it in a complex app with lots of routes, but i am pretty confident, that it will work fine. Maybe someone has a more lean way of doing this :-)

8
votes

Ok. Got this solved. It seems that the currentPath is set in the applicationController instance. So I did this:

App = Em.Application.create({

    currentPath: '',

    ApplicationController : Ember.Controller.extend({
        updateCurrentPath: function() {
            App.set('currentPath', this.get('currentPath'));
        }.observes('currentPath')
    }),

    ...

});

Then, I can bind or observe the App.currentPath from anywhere in the code and react on its changes.

0
votes

This example from my component. All entity have property - container.

GetCurrentPath:-> app = @container.lookup('controller:application') return app.get 'currentPath'