51

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

<ul class="nav nav-tabs">
    <li class="active"><a href="#">Home</a></li>
    <li><a href="#">Profile</a></li>
    <li><a href="#">Messages</a></li>
</ul>

The active tab is marked with class="active".

There is great example of static navbar and Router/Outlet feature at http://jsfiddle.net/schawaska/pfbva/, but I can't understand how to create a dynamic navbar/menu/tab view.

As far as I understand, it is possible to use class bindings in each menu item:

 classNameBindings: ['isActive:active']

But where is the right place to switch isActive attributes ?

James A. Rosen
  • 64,193
  • 61
  • 179
  • 261
coxx
  • 1,019
  • 2
  • 9
  • 7
  • 7
    FYI: In the new router, `{{linkTo}}` provides this behavior automatically -- http://emberjs.com/guides/routing/defining-your-routes/ – Yehuda Katz Jan 11 '13 at 07:39

15 Answers15

154

Ember 1.11+:

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

Ember < 1.11 (bind-attr required):

{{#link-to "dashboard" tagName="li"}}
  <a {{bind-attr href="view.href"}}>Dashboard</a>
{{/link-to}}
Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
lesyk
  • 3,979
  • 3
  • 25
  • 39
  • 18
    This needs to be the accepted answer. I just spent 10 minutes trying to get a `bindAttr` on an
  • tag to reference the view.href by name until I found this and realized I had it all backwards.
  • – jonnii Feb 12 '13 at 18:57
  • @jonnii Agree, I've posted a full solution below combining the two pieces. – Terry Feb 14 '13 at 21:18
  • 7
    This is definitely the easiest way to do it – Willem de Wit Feb 15 '13 at 09:41
  • The problem is that this approach doesn't work with a nested menu – Willem de Wit Feb 15 '13 at 15:37
  • I guess with a nested menu, you'll need some specific component to handle it for you. Anyway this is the cleanest and easiest way to do it in the context of the original question. – Bastes Feb 26 '13 at 12:22
  • 3
    Definitely the best answer I found out of the group. – Zach Riggle Mar 26 '13 at 04:38
  • 4
    This is definately the most easiest way to do it! Should be the accepted answer. – roman May 24 '13 at 10:13
  • Just adding my support. A much better answer. – Jagu Sep 10 '13 at 05:29
  • 3
    How exactly is the poor soul who comes across my code after I've inserted this oddity supposed to know that it will insert an 'active' class in the li? Very unclear. – davidbuttar Sep 16 '13 at 11:36
  • 5
    You'll want to explicitly style your cursor to be a pointer when over the link element: `a {cursor: pointer;}` See http://marceldegraaf.net/2013/05/17/ember-js-and-bootstrap-navbar-mark-li-active.html – Gabriel Grant Dec 08 '13 at 02:18
  • 3
    AFAIK this solution will not work anymore as `href` wont be generated for a non `a` tags: http://discuss.emberjs.com/t/tagname-and-html-best-practices/4260/3 – chrmod Mar 04 '14 at 10:56