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

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

    Building on katz' answer, you can have the active property be recomputed when the nav element's parentView is clicked.

    App.NavView = Em.View.extend({
    
        tagName: 'li',
        classNameBindings: 'active'.w(),
    
        didInsertElement: function () {
            this._super();
            var _this = this;
            this.get('parentView').on('click', function () {
                _this.notifyPropertyChange('active');
            });
        },
    
        active: function () {
            return this.get('childViews.firstObject.active');
        }.property()
    });
    

提交回复
热议问题