1

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?

Community
  • 1
  • 1
Roman Semko
  • 1,541
  • 5
  • 18
  • 27

4 Answers4

11

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');
    }
})
Meori Oransky
  • 688
  • 4
  • 8
  • This technique is [found in the documentation](http://emberjs.com/guides/understanding-ember/debugging/#toc_get-current-route-name-path) – dwhite Jan 29 '14 at 14:14
9

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 :-)

mavilein
  • 11,648
  • 4
  • 43
  • 48
8

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.

Roman Semko
  • 1,541
  • 5
  • 18
  • 27
0

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

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

DenQ
  • 89
  • 1
  • 6