Search code examples
ember.jsember-router

Using the new Ember RouterV2, how do you immediately redirect to another state from the index state?


What I have so far:

App = Ember.Application.create({
    LOG_TRANSITIONS: true
});


App.Router.map(function(match){
    match('/').to('application');
    match('/edit').to('edit');
});


App.ApplicationRoute = Ember.Route.extend({
    redirect: function() {
        this.transitionTo('edit');
    },
    events: {
        startEdit: function( context ){
            this.transitionTo( 'edit' );
        }
    }
})

App.EditRoute = Ember.Route.extend({
    init: function(){
        this._super()
        console.log('EditRoute')
    },
});

Handlebars:

<script type="text/x-handlebars" data-template-name = 'application'>
    Hello World
    {{ outlet main }} 
</script>

<script type="text/x-handlebars" data-template-name = 'edit'>
    <div class = 'edit-background'> Edit State: {{ title }} </div>
</script>

I have four questions:

  1. When I open the application it just remains in the home page, is the redirectTo hook suppose to immediately redirect you to another state?

  2. In addition, I have this events hash in AplicationRoute per suggestion from here: How to programmatically transition between routes using Ember.js' new Router. but I read through the answers and still am not sure how you are supposed to use it.

  3. How do I test the router on the console? before you could navigate between the states by calling transitionTo commands, what do I do now?

  4. For some odd reason, my application template seem to rendered twice, as in there are two 'Hello World' up there, and when try to add something like: <li>{{#linkTo edit}}edit{{/linkTo}}</li>

I get this error:

'Uncaught TypeError: Cannot read property 'container' of undefined   --  ember.js:2223' 

Solution

  • This is how you would initially load the editView/route/template on application start up:

    Router

    App.Router.map(function(match){
      match('/').to('application',function(match){
        match('/').to('edit')
      })
    })
    

    ApplicationTemplate

    <script type="text/x-handlebars" data-template-name="application">
        {{outlet}}
    </script>
    

    EditTemplate

    <script type="text/x-handlebars" data-template-name="edit">
        I am embedded! 
    </script>
    

    EditRoute

    EditRoute = Ember.Route.extend({
      renderTemplates:function () {
          this.render('edit', {
          into:'application'
       });
    })