Redirect to first item in ArrayController

I am trying to redirect to the first element in an ArrayController. I found several other questions related to this, but no one had the answers that seemed to work (there were a lot of changes, so this is understandable).

One specific answer from Yehuda is here :

App.DoctorsRoute = Ember.Route.extend({
  model: function() {
    return App.Doctor.find();
  },

  redirect: function() {
    var doctor = this.modelFor('doctors').get('firstObject');
    this.transitionTo('doctor', doctor);
  }
});

I think: "I recreated this script, but I must have done something wrong ...

Any understanding of what I am doing wrong is much appreciated.

JSBin example here .

+5
source share
1 answer

, . promis , abit, .

App.DoctorsRoute = Ember.Route.extend({
  model: function() {
    return App.Doctor.find().then(function (list) {
       return list.get('firstObject');
    });
  },

  redirect: function() {
    var doctor = this.modelFor('doctors');
    this.transitionTo('doctor', doctor);
  }
});

of the course.. wel, redirect abit , , , :

App.DoctorsRoute = Ember.Route.extend({
  model: function() {
    return App.Doctor.find();
  },

  redirect: function() {
    var self = this;
    this.modelFor('doctors').then(function (list) {
           return list.get('firstObject');
       }).then(function (doctor){
           if(!doctor)
              self.transitionTo('doctor', doctor);
       });
  }
});
+1

All Articles