Ember.js afterModel hook launches two identical requests

in my application, I need to be redirected to the latest model when visiting the root, which in this case is always the first object 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() {
    return this.store.find('thread');
  },
  afterModel: function(threads) {
    this.transitionTo('threads.view', threads.get('firstObject'));
  }
});

This works without problems, but if I go straight to the root URL or view, 2 identical requests will be made to /threads. As soon as I comment on the section afterModel, the redirect obviously doesn't work anymore, but the queries return to 1.

Any help is gladly accepted!

+4
source share
2 answers

Threads/View , ThreadsRoute View.

, ThreadsRoute β†’ ThreadsIndexRoute afterModel hooks :

( )

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

:

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

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

App.ThreadsIndexRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('thread');
  }
});

, , '/', , threads.index, , , . , ThreadsIndexRoute (, ThreadsRoute ThreadsIndexRoute)

+2

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


All Articles