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

前端 未结 9 1600
天命终不由人
天命终不由人 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:34

    Current answers at time of writing are dated. In later versions of Ember if you are using {{link-to}} it automatically sets 'active' class on the <a> element when the current route matches the target link.

    So just write your css with the expectation that the <a> will have active and it should do this out of the box.

    Lucky that feature is added. All of the stuff here which was required to solve this "problem" prior is pretty ridiculous.

    0 讨论(0)
  • I recreated the logic used internally. The other methods seemed more hackish. This will also make it easier to reuse the logic elsewhere I might not need routing.

    Used like this.

    {{#view App.LinkView route="app.route" content="item"}}{{item.name}}{{/view}}
    
    App.LinkView = Ember.View.extend({
        tagName: 'li',
        classNameBindings: ['active'],
        active: Ember.computed(function() {
          var router = this.get('router'),
          route = this.get('route'),
          model = this.get('content');
          params = [route];
    
          if(model){
            params.push(model);
          }
    
          return router.isActive.apply(router, params);
        }).property('router.url'),
        router: Ember.computed(function() {
          return this.get('controller').container.lookup('router:main');
        }),
        click: function(){
            var router = this.get('router'),
            route = this.get('route'),
            model = this.get('content');
            params = [route];
    
            if(model){
                params.push(model);
            }
    
            router.transitionTo.apply(router,params);
        }
    });
    
    0 讨论(0)
  • 2020-12-08 01:41

    You can also use nested link-to's:

    {{#link-to "ccprPracticeSession.info" controller.controllers.ccprPatient.content content tagName='li' href=false eventName='dummy'}}
      {{#link-to "ccprPracticeSession.info" controller.controllers.ccprPatient.content content}}Info{{/link-to}}
    {{/link-to}}
    
    0 讨论(0)
  • 2020-12-08 01:41

    You can skip extending a view and use the following.

    {{#linkTo "index" tagName="li"}}<a>Homes</a>{{/linkTo}}
    

    Even without a href Ember.JS will still know how to hook on to the LI elements.

    0 讨论(0)
  • 2020-12-08 01:41

    For the same problem here I came with jQuery based solution not sure about performance penalties but it is working out of the box. I reopen Ember.LinkView and extended it.

    Ember.LinkView.reopen({
        didInsertElement: function(){
            var el = this.$();
    
            if(el.hasClass('active')){
                el.parent().addClass('active');
            }
    
            el.click(function(e){
                el.parent().addClass('active').siblings().removeClass('active');
            });
        }
    });
    
    0 讨论(0)
  • 2020-12-08 01:47

    I have just written a component to make this a bit nicer:

    App.LinkLiComponent = Em.Component.extend({
      tagName: 'li',
      classNameBindings: ['active'],
      active: function() {
        return this.get('childViews').anyBy('active');
      }.property('childViews.@each.active')
    });
    
    Em.Handlebars.helper('link-li', App.LinkLiComponent);
    

    Usage:

    {{#link-li}}
      {{#link-to "someRoute"}}Click Me{{/link-to}}
    {{/link-li}}
    
    0 讨论(0)
提交回复
热议问题