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

微笑、不失礼 提交于 2019-12-30 08:32:21

问题


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

Any ideas?


回答1:


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')
});



回答2:


This is a total hack, but it works in Ember 1.0.0. To make links to users stay active when the user route is active:

App.UserRoute = Ember.Route.extend({

    activate: function() {
        setTimeout(function() {
            $('[href="#/users"]').addClass("active");
        }, 0);
    }
});


来源:https://stackoverflow.com/questions/18473807/is-there-a-way-to-pass-an-array-to-currentwhen-in-emberjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!