Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to pass an array to currentWhen in EmberJS?

Tags:

ember.js

I'm trying to make a link stay 'active' on multiple routes, such as /#/users and /#/user.

Any ideas?

like image 749
Jarrod Taylor Avatar asked Aug 27 '13 19:08

Jarrod Taylor


1 Answers

You can reopen Ember's LinkView and do something like this (allows currentWhen to contain space delimited values)

Ember.LinkView.reopen({
    active: function() {
        // we allow link-to's currentWhen to specify multiple routes
        // so we need to check each one of them
        var loadedParams = Ember.get(this, 'loadedParams');
        var currentWhen = this['current-when'] || this.currentWhen;
        currentWhen = currentWhen || (loadedParams && loadedParams.targetRouteName);

        if (currentWhen && currentWhen.indexOf(' ') >= 0) {
                var currents = currentWhen.split(' ');
                router = Ember.get(this, 'router');

            for (var i = 0; i < currents.length; i++) {
                var isActive = router.isActive.apply(router, [currents[i]]);
                if (isActive)
                    return isActive;
            }

            return false;
        }

        return this._super();
    }.property('resolvedParams', 'routerArgs')
});
like image 189
Andreas Andreou Avatar answered Oct 12 '22 01:10

Andreas Andreou