emberjs - how to mark active menu item using router infrastructure

前端 未结 15 1409
悲&欢浪女
悲&欢浪女 2020-11-27 13:06

I\'m trying to create navigation tabs (taken from Twitter Bootstrap):

相关标签:
15条回答
  • 2020-11-27 13:49

    Here's a full working solution:

    View:

    App.NavView = Ember.View.extend({
      tagName: 'li',
      classNameBindings: ['active'],
    
      active: function() {
        return this.get('childViews.firstObject.active');
      }.property()
    });
    

    Template:

    <ul>
      {{#each item in controller}}
      {{#view App.NavView}}
      {{#linkTo "item" item tagName="li"}}
          <a {{bindAttr href="view.href"}}>
            {{ item.name }}
          </a>
      {{/linkTo}}
      {{/view}}
      {{/each}}
    </ul>
    
    0 讨论(0)
  • 2020-11-27 13:51

    baijum's answer above is mostly correct however in the latest versions of Ember the "bind-attr" is deprecated. Here is the new way to write it:

    {{#link-to "dashboard" tagName="li"}}
        <a href="{{view.href}}">Dashboard</a>
    {{/link-to}}
    

    As you can see, it's even easier and kind of works like magic..

    0 讨论(0)
  • 2020-11-27 13:52

    Recently an Ember-cli addon came available to just do this. It is called ember-cli-active-link-wrapper.

    Install: ember install ember-cli-active-link-wrapper

    You can use it like this:

    {{#active-link}}
      {{link-to "Index" "index"}}
    {{/active-link}}
    

    which results in:

    <li class='active'>
        <a href="/" class='active'>Index</a>
    </li>
    
    0 讨论(0)
提交回复
热议问题