【发布时间】:2014-10-10 19:09:34
【问题描述】:
我注意到在使用 EmberJS 时,如果您所在的路由与页面上具有相同路径的链接匹配,它会自动采用“活动”类。
我目前正在构建一个使用它的导航栏,但我担心并非所有路线都与我的导航栏匹配。
例如,当我在路线/messages 中时,我想让项目/services 处于活动状态。
或者当我在路线/forums/history 中时,我想让项目/forums/archives 处于活动状态。即使这条路线不在导航中,我也想突出显示与之相关的项目。
我的导航是从一个看起来像这样的 json 对象生成的:
[
{
"label": "Forums",
"link": "forums",
"children": [
{
"label": "Latest"
"link": "forums.latest"
}
{
"label": "Archives"
"link": "forums.archives"
}
]
}
]
我有几个我不喜欢的想法,这就是为什么我来这里征求你的建议。以下是我的想法:
- 在视图中定义一个属性,以定义导航中应处于活动状态的项目:
观点/论坛/history.js:
Ember.View.extend({
navigationActiveItem: 'forums.archives'
});
- 在我的 json 对象中定义将突出显示该项目的链接列表
json 文件:
[
{
"label": "Forums",
"link": "forums",
"children": [
{
"label": "Archives"
"link": "forums.archives",
"makeActive": ["forums.history", "forums.records"] //If you are on one of those route, it will active forums.archives
}
]
}
]
router.js:
Router.map(function () {
this.route('forums', function () {
this.route('latest');
this.route('archives');
this.route('history');
});
});
你有什么更好的建议来做这样的事情吗? 谢谢
【问题讨论】:
-
你能添加你的路由器代码吗?如果它们正确嵌套,导航将获得活动类
-
我用路由器编辑了问题。
-
历史与存档有何关系,服务和消息在哪里?
-
这些只是我需要的示例。服务和消息与论坛处于同一水平。历史与档案有关,因为我希望它是。
-
嵌套路由是一种选择吗?
标签: javascript ember.js