How do I bind to the active class of a link using the new Ember router?

前端 未结 9 1616
天命终不由人
天命终不由人 2020-12-08 01:17

I\'m using Twitter Bootstrap for navigation in my Ember.js app. Bootstrap uses an active class on the li tag that wraps navigation links, rather th

9条回答
  •  佛祖请我去吃肉
    2020-12-08 01:49

    We definitely need a more public, permanent solution, but something like this should work for now.

    The template:

      {{#view App.NavView}} {{#linkTo "about"}}About{{/linkTo}} {{/view}} {{#view App.NavView}} {{#linkTo "contacts"}}Contacts{{/linkTo}} {{/view}}

    The view definition:

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

    This relies on a couple of constraints:

    • The nav view contains a single, static child view
    • You are able to use a view for your
    • s. There's a lot of detail in the docs about how to customize a view's element from its JavaScript definition or from Handlebars.

    I have supplied a live JSBin of this working.

提交回复
热议问题