Ember.js routing: how do you set the default route for rendering right away?

I am sure this will become clear as I dig deeper, but it is not yet clear how to do this.

I followed the information about this useful article on routing , but the important part is missing in this example, i.e. how do you get a home view for immediate rendering without clicking the home link?

I began to delve into the documents to try to understand this, but meanwhile it seems that a useful question answered the offspring.

I played with a working jsfiddle example from the above question here and comparing with this other example, I found that the work seems to work by default

This is still a mystery.

Current code :

App.Router = Em.Router.extend({ enableLogging: true, location: 'hash', root: Em.State.extend({ // EVENTS goHome: Ember.State.transitionTo('home'), viewProfile: Ember.State.transitionTo('profile'), // STATES index: Em.State.extend({ route: "/", redirectsTo: 'home' }), home: Em.State.extend({ route: '/home', connectOutlets: function(router, context) { var appController = router.get('applicationController'); appController.connectOutlet('home'); } }), // STATES profile: Em.State.extend({ route: '/profile', connectOutlets: function(router, context) { var appController = router.get('applicationController'); appController.connectOutlet('profile'); } }), doOne: function() { alert("eins"); } }) }); 

UPDATE: Solution

It turns out the reason I worked did not work because she was using Em.State.extend and not Em.Route.extend . The interesting part is that when I step by step and change them one by one, the example does not work until I change all of them.

Here is a working example :

 App.Router = Em.Router.extend({ enableLogging: true, location: 'hash', root: Em.Route.extend({ // EVENTS goHome: Ember.State.transitionTo('home'), viewProfile: Ember.State.transitionTo('profile'), // STATES index: Em.Route.extend({ route: "/", redirectsTo: 'home' }), home: Em.Route.extend({ route: '/home', connectOutlets: function(router, context) { var appController = router.get('applicationController'); appController.connectOutlet({name: 'home'}); } }), // STATES profile: Em.Route.extend({ route: '/profile', connectOutlets: function(router, context) { var appController = router.get('applicationController'); appController.connectOutlet('profile'); } }), doOne: function() { alert("eins"); } }) }); 
+6
source share
3 answers

U can do a redirect from index to home route. Working example: http://jsfiddle.net/FRfBt/

+3
source

It seems now this is being done with difficulty. I have succeeded in this:

 App = Ember.Application.create(); App.Router.map(function() { // 'index' route is default this.resource('dashboard'); }); App.IndexRoute = Ember.Route.extend({ redirect: function() { // this redirects / to /dashboard this.transitionTo('dashboard'); } }); App.DashboardRoute = Ember.Route.extend({ }); 
+9
source

With the Ember CLI, you can put redirect in index.js in your root of the routes directory:

 import Ember from 'ember'; export default Ember.Route.extend( { redirect: function() { this.transitionTo('dashboard'); } }); 
+6
source

Source: https://habr.com/ru/post/918803/


All Articles