javascript - Ember: WillTransition does not fire when going directly to URL -


i need perform authorization on routes user allowed access. according documentation ember should use 'willtransition'

http://guides.emberjs.com/v1.11.0/routing/preventing-and-retrying-transitions/

when transition attempted, whether via {{link-to}}, transitionto, or url change, willtransition action fired on active routes. gives each active route, starting leaf-most route, opportunity decide whether or not transition should occur.

so put code in application route try , first log times called.

   actions: {         willtransition: function(){             console.log('transitioning');         }     } 

this works fine when i'm inside app , click transition route. if go directly protected route doesn't fire. ex. /myapp/protected/route

but debug flag

nv.app.log_transitions = true; 

set these logs inside console. when 'willtransition' event hasn't fired.

preparing transition 'myapp.index' 'myapp.protected.index' transitioned 'myapp.protected.index' 

so go , @ log event in ember source , see

/**   handles notifying listeners of impending url   change.    triggers router level `willtransition` hook.    @method willtransition   @private   @since 1.11.0 */ willtransition: function (oldinfos, newinfos, transition) {   run['default'].once(this, this.trigger, "willtransition", transition);    if (property_get.get(this, "namespace").log_transitions) {     ember['default'].logger.log("preparing transition '" + emberrouter._routepath(oldinfos) + "' '" + emberrouter._routepath(newinfos) + "'");   } }, 

right above line writes console log line looks supposed fire event, not reaching code. doing wrong?

authorization check might better directly on routes, in beforemodel function.

you can create mixin , implement on every route

app.auth =  ember.mixin.create({    beforemodel: function(){        if(!this.get('userauthorized')){            //prevent entering , go login route            this.transitionto('login');            //show msg user needs access        }    } }); 

you can implement mixin on routes this

app.userstuffroute = ember.route.extend(app.auth,{ }); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -