Sunday, 15 May 2011

javascript - Ember.js: redirect in router if certain condition is satisfied in controller? -


In fact, the purpose of the login to the user account is to present the account page, otherwise it is redirected to a login page.

I have the following routes:

  App.Router.map (function () {this.resource ('account', {path: '/'}); This 'root' attempts to access a  log in  attribute in the account administrator in my current code path: 'login', {path: '/ login'})}}  

  App.AccountRoute = Ember.Route.extend ({renderTemplate: function (controller) {var log = = controller ("log in"); // Error: Controller undefined if (! LoggedIn) {This.transitionTo ('Login');}}});  

Do I want to apply this argument to the router Why is the controller undefined in my way? Thanks!

These are some suggestions that help you

  1. Your administrator does not always exist, it is created by amber when it is needed for the first time you can see which controllers have already been created, amber You can use Chrome extension for debugging, because in your case it should be available, because You are in the renderTemplate hook In general, the redirect should be done either in the first model code hook or redirect hook:

    < Pre> Redirect: function () (if (! This. Controller.get ('Login')) {this.transitionTo ('Login'); }}
  2. Consider moving the authentication logic to an amber service (). In Amber, a service is only one class which extends Ember.Object . You will have the capability of that service in all your controllers and routes, so it will always be available.

  3. Even better: Consider using the best that handles both authentication and authorization, it will provide a session service anywhere in your app , So that you can be able to do things:

      // This ensures that the user is authenticated (! This.get ('session.isAuthenticated')) {this.transitionTo ( 'Login'); }  

    Or even better (since you do not want to paste that stuff everywhere):

      // This route is now authenticated! App.AccountRoute = Ember.Route.extend (AuthenticatedRouteMixin, {...}  

And many other good things!

Its Besides, I think you are not using yet. I will recommend it when you feel more comfortable with amber. Amber CLI is the future of amber, it comes with slightly different syntax but is very great Things are.


No comments:

Post a Comment