Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

ember.js - Emberjs: Conditional redirect in router

Is there a way to have a conditional redirect in the Ember.js Router, without breaking internal consistency of the router?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you could do (as of today), is something like that:

root: Ember.Route.extend({
    index: Ember.Route.extend({
        enter: function(router) {
            var logged = /* get from appropriated source... */;
            Ember.run.next(function() {
                if (logged) {
                    router.transitionTo('loggedIn');
                } else {
                    router.transitionTo('loggedOut');
                }
            });
        }
    }),

    loggedIn: Ember.Route.extend({
        // ...
    }),

    loggedOut: Ember.Route.extend({
        // ...
    })
})

Do not miss the Ember.run.next as while you are in enter, the state transition is always pending, so you have to transition after that.

We use it as shown for authent, but you could imagine using it for whatever condition you have to...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...