获取Ember.js中的路由列表 [英] Getting a list of routes in Ember.js
问题描述
在我的主页面( index.hbs
)中,我需要显示与给定条件匹配的每条路线的链接列表(例如,具有某个属性) 。所以我需要的是这样的:
On my main page (index.hbs
), I need to display a list of links to each route which matches a given criteria, (e.g. has a certain attribute). So what I need is something like this:
// Define a route with some attribute
App.FirstRoute = Ember.Route.extend({
showOnIndex: true
});
// Get a list of visible routes in the IndexController
visibleRoutes: function() {
var routes = /* How to do this */
return routes.filter(function(route) {
route.get('showOnIndex'));
});
}.property()
问题是如何获得路由
。我发现我可以通过以下方式获取路由名称列表:
The problem is how to get routes
. I have found that I can get a list of route names by:
var router = this.get('target');
var names = router.router.recognizer.names;
但是我找不到如何将它们翻译成 Route
对象。据推测,
Router
具有这样的信息,但是AFAICS不是公开暴露的。有什么建议么?
But I can't find out how to translate them into Route
objects. Presumably the Router
has the information to do so, but AFAICS it is not publically exposed. Any suggestions?
推荐答案
如何使用容器和查找?
Ember.keys(App.Router.router.recognizer.names).map(function(name) {
return App.__container__.lookup('route:' + name);
}).compact();
我知道您访问 __ container __
私人的。但通常你不应该访问所有路线。
I know you access the __container__
which is private. But normally you shouldn't access all the routes anyway.
这篇关于获取Ember.js中的路由列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!