我想要监控App.Router.router.currentState,以便激活/停用导航链接。这里所解释的内容:https://stackoverflow.com/a/13312466/674525
但是,属性currentState似乎不再存在于路由器中。App.get('Router.router.currentState')返回未定义的。
我想,在最近的Ember版本中,它发生了变化。有没有其他方法可以做到这一点?
发布于 2013-02-24 18:26:18
好的。解决了这个问题。似乎在applicationController实例中设置了currentPath。所以我这样做了:
App = Em.Application.create({
currentPath: '',
ApplicationController : Ember.Controller.extend({
updateCurrentPath: function() {
App.set('currentPath', this.get('currentPath'));
}.observes('currentPath')
}),
...
});然后,我可以从代码中的任何位置绑定或观察App.currentPath,并对其更改做出反应。
发布于 2013-06-05 16:21:44
我相信使用新的Ember构建来做到这一点的“教科书”方法是:
App.SomeController = Ember.Controller.extend({
needs: 'application',
someFunction: function(){
var state = this.get('controllers.application.currentPath');
}
})发布于 2013-02-22 17:24:19
对于我来说,这在当前版本中变得有点复杂,但至少它可以工作:
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这段代码的灵感来自于阅读这个Ember Source。我还没有在有很多路线的复杂应用程序中测试它,但我非常有信心,它会工作得很好。也许有人有一种更精干的方法来做这件事:-)
https://stackoverflow.com/questions/15019212
复制相似问题