I do not think that the router should handle the action because the request to try to log in is not a state change.
I think that is exactly the case: an attempt to login should go into authenticating state, where, for example, another click on "login" is ignored.
Therefore, IMHO must be handled by the router. I am thinking of something similar, see http://jsfiddle.net/pangratz666/97Uyh/ :
Steering wheels :
<script type="text/x-handlebars" > {{outlet}} </script> <script type="text/x-handlebars" data-template-name="login" > <p class="info">{{message}}</p> Login to view the admin area <br/> Email: {{view Ember.TextField valueBinding="email" }} <br/> Password: {{view Ember.TextField valueBinding="password" }} <br/> <button {{action login}} >Login</button> </script> <script type="text/x-handlebars" data-template-name="authenticating" > Communicating with server ... </script> <script type="text/x-handlebars" data-template-name="admin" > Hello admin! </script>
JavaScript :
App = Ember.Application.create(); App.ApplicationController = Ember.Controller.extend({ login: function() { // reset message this.set('message', null); // get data from login form var loginProps = this.getProperties('email', 'password'); // simulate communication with server Ember.run.later(this, function() { if (loginProps.password === 'admin') { this.set('isAuthenticated', true); this.get('target').send('isAuthenticated'); } else { this.set('message', 'Invalid username or password'); this.set('isAuthenticated', false); this.get('target').send('isNotAuthenticated'); } }, 1000); // inform target that authentication is in progress this.get('target').send('authenticationInProgress'); }, logout: function() { this.set('isAuthenticated', false); } }); App.ApplicationView = Ember.View.extend({ templateName: 'application' }); App.LoginView = Ember.View.extend({ templateName: 'login' }); App.AdminView = Ember.View.extend({ templateName: 'admin' }); App.AuthenticatingView = Ember.View.extend({ templateName: 'authenticating' }); App.Router = Ember.Router.extend({ root: Ember.Route.extend({ index: Ember.Route.extend({ route: '/', loggedOut: Ember.Route.extend({ route: '/', connectOutlets: function(router) { router.get('applicationController').connectOutlet('login'); }, login: function(router) { router.get('applicationController').login(); }, authenticationInProgress: function(router) { router.transitionTo('authenticating'); } }), authenticating: Ember.State.extend({ enter: function(router) { router.get('applicationController').connectOutlet('authenticating'); }, isAuthenticated: function(router) { router.transitionTo('loggedIn'); }, isNotAuthenticated: function(router) { router.transitionTo('loggedOut'); } }), loggedIn: Ember.Route.extend({ route: '/admin', connectOutlets: function(router) { if (!router.get('applicationController.isAuthenticated')) { router.transitionTo('loggedOut'); } router.get('applicationController').connectOutlet('admin'); }, logout: function(router) { router.get('applicationController').logout(); } }) }) }) });โ
pangratz
source share