Ember App.Router.router.currentState undefined

Rom*_*mko 1 ember.js

我想监视App.Router.router.currentState以激活/停用导航链接.如下所述:https://stackoverflow.com/a/13312466/674525

但是,属性currentState似乎不再存在于路由器中.App.get('Router.router.currentState')返回undefined.

我想,它在最近的Ember版本中发生了变化.还有另一种方法吗?

Meo*_*sky 11

我相信使用新的Ember版本的"教科书"方式将是:

App.SomeController = Ember.Controller.extend({
    needs: 'application',
    someFunction: function(){
        var state = this.get('controllers.application.currentPath');
    }
})
Run Code Online (Sandbox Code Playgroud)


mav*_*ein 9

这对我来说当前的版本有点复杂,但至少它有效:

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
Run Code Online (Sandbox Code Playgroud)

此代码的灵感来自阅读此Ember Source.我还没有在一个复杂的应用程序中测试它有很多路线,但我很自信,它会工作正常.也许某人有更精益的方式这样做:-)


Rom*_*mko 8

好.得到了解决.似乎currentPath是在applicationController实例中设置的.所以我这样做了:

App = Em.Application.create({

    currentPath: '',

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

    ...

});
Run Code Online (Sandbox Code Playgroud)

然后,我可以从代码中的任何位置绑定或观察App.currentPath并对其更改做出反应.