emberjs - how to mark active menu item using router infrastructure

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

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

15条回答
  •  天命终不由人
    2020-11-27 13:35

    Not sure if it's very dynamic but try to see solution at http://codebrief.com/2012/07/anatomy-of-an-ember-dot-js-app-part-i-redux-routing-and-outlets/ The main idea is to check state of your app

    JavaScript:

    function stateFlag(name) {
      return Ember.computed(function() {
        var state = App.router.currentState;
        while(state) {
          if(state.name === name) return true;
          state = state.get('parentState');
        }
        return false;
      }).property('App.router.currentState');
    }
    
    ApplicationController: Ember.Controller.extend({
        isHome: stateFlag('home'),
        isSections: stateFlag('sections'),
        isItems: stateFlag('items')
      })
    

    Handlebars:

  • Update: pangratz's solution looks prettier

提交回复
热议问题