首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ember App.Router.router.currentState未定义

Ember App.Router.router.currentState未定义
EN

Stack Overflow用户
提问于 2013-02-22 15:43:20
回答 4查看 6.4K关注 0票数 1

我想要监控App.Router.router.currentState,以便激活/停用导航链接。这里所解释的内容:https://stackoverflow.com/a/13312466/674525

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

我想,在最近的Ember版本中,它发生了变化。有没有其他方法可以做到这一点?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-02-24 18:26:18

好的。解决了这个问题。似乎在applicationController实例中设置了currentPath。所以我这样做了:

代码语言:javascript
复制
App = Em.Application.create({

    currentPath: '',

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

    ...

});

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

票数 8
EN

Stack Overflow用户

发布于 2013-06-05 16:21:44

我相信使用新的Ember构建来做到这一点的“教科书”方法是:

代码语言:javascript
复制
App.SomeController = Ember.Controller.extend({
    needs: 'application',
    someFunction: function(){
        var state = this.get('controllers.application.currentPath');
    }
})
票数 11
EN

Stack Overflow用户

发布于 2013-02-22 17:24:19

对于我来说,这在当前版本中变得有点复杂,但至少它可以工作:

代码语言:javascript
复制
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。我还没有在有很多路线的复杂应用程序中测试它,但我非常有信心,它会工作得很好。也许有人有一种更精干的方法来做这件事:-)

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15019212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档