简体   繁体   English

Ember.js-在路线更改时更新导航栏

[英]Ember.js - Updating navigation bar on route change

I'm new to Ember.js and still trying to wrap my head around it. 我是Ember.js的新手,仍在努力将它包裹住。

One of things I've been trying to do lately is changing the buttons of a navigation bar depending on which route I am. 我最近一直在尝试做的一件事就是根据我所走的路线更改导航栏的按钮。 My application has two levels of navigation. 我的应用程序具有两个导航级别。 The first level is a bar which allows the user to navigate through the main tabs of the application, and this changes the router. 第一级是一个栏,它使用户可以浏览应用程序的主要选项卡,从而更改路由器。 Then inside each of those tabs, there is a sub-navigation bar which allows the user to navigate to more specific tabs inside the first-level tabs. 然后,在每个选项卡中,都有一个子导航栏,允许用户导航到第一级选项卡中的更特定的选项卡。

I understand how nested routing works, but I'm not sure how to implement such a navigation system. 我了解嵌套路由的工作原理,但不确定如何实现这种导航系统。 What I want to do is update the sub-navigation bar with the labels specific to the 1st level tab the user is in. 我要执行的操作是使用特定于用户所在的“第一级”标签的标签更新子导航栏。

Any ideas? 有任何想法吗?

Update with code for the navigation/routes: 使用导航/路线的代码更新:

App.Router.map(function() {  
    this.resource("spots", function() {  
        this.route("aroundYou");
        this.route("search");  
    });  
    this.resource("rankings", function() {  
        this.route("topUsers");  
        this.route("topSpots");  
    }  
});

You can handle this programmatically by keeping track of all your menuitems and submenuitems (these are more or less your routes) in a navigation controller. 您可以通过在导航控制器中跟踪所有菜单项和子菜单项(或多或少的路线)来以编程方式进行处理。 The navigation template displays all of the menuitems and the submenuitems of the active menuitem. 导航模板显示活动菜单项的所有菜单项和子菜单。 Clicking on a menuitem changes the active menuitem, and clicking on a submenuitem triggers an navigateTo action, in which you can do the logic to handle the transitioning. 单击菜单项会更改活动的菜单项,然后单击子菜单会触发navigateTo动作,您可以在其中执行处理过渡的逻辑。

NavigationController: NavigationController:

navigationItems: ["spots, rankings"]
subNavigationItems: {
  spots: [ "aroundYou" , "search" ]
  rankings: ["topUsers", "topSpots"]
}

// `newItem` is the navigation menu item the user clicked on
selectNavigationItem: function(newItem) {
    this.set('currentNavigationItem', newItem);
}

// `target` is a string from subNavigationItems that gets passed through the action helper when the user clicks on a submenu item
navigateTo: function(target) {
  // construct the route from the active navigation item and the chosen subitem
  route = this.get('currentNavigationItem') + "." + target
  this.transitionToRoute(route)
}

currentNavigationItem: null // or whatever default

// change the subnavigation items to display based on the current navigation path
currentSubNavigationItems: function() {
  key = this.get('currentNavigationItem')
  return this.get('subitems')[key]
}.property('currentNavigationItem')

navigation.hbs: navigation.hbs:

<div class="navigation">
{{#each navigationItem in navigationItems}}
   <a {{action selectNavigationItem navigationItem}}> {{navigationItem}} </a>
{{/each}}
</div>
<div class="subnavigation">
{{#each subNavigationItem in currentSubNavigationItems}}
   <a {{action subNavigateTo subNavigationItem}}> {{subNavigationItem}} </a>
{{/each}}
</div>

Let me know if that's unclear. 让我知道是否不清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM