afterModel hook triggers two identical requests


#1

Hi all
In my app I need that when I visit the root, it redirects to the view of the most recent model that in this case is always the firstObject in the collection.

App.Router.map(function() {
    this.resource('threads', { path: '/' }, function() {
        this.route('view', { path: ':thread_id' });
    });
});

App.ThreadsRoute = Ember.Route.extend({
  model: function() {
    // console.log('in model);
    return this.store.find('thread');
  },
  afterModel: function(threads) {
    // console.log('in afterModel);
    this.transitionTo('threads.view', threads.get('firstObject'));
  }
});

This is working without problems, but wheter I directly go to the root url or to the view url, 2 identical requests to /threads are made. With the help of console.log() we can see that model and afterModel are both triggered 2 times. As soon I comment the afterModel section the redirection obviously doesn’t work anymore but the requests are back to 1.

Moreover according to this Changelog and the doc about transitionTo the child model hook should never be triggered, shouldn’t it?

Any help is gladly accepted!


#2

Since Threads/View are nested routes, the ThreadsRoute will be also called on the View route.

I think you should just call the ThreadsRoute -> ThreadsIndexRoute or separate model and afterModel hooks this way:

App.ThreadsRoute = Ember.Route.extend({
  model: function() {
    // console.log('in model);
    return this.store.find('thread');
  }
});

App.ThreadsIndexRoute = Ember.Route.extend({
  afterModel: function(threads) {
   // console.log('in afterModel);
    this.transitionTo('threads.view', threads.get('firstObject'));
  }
});

#3

That’s worked perfectly! You’re my hero, I knew I was doing something wrong but very stupid XD