Javascript 重定向到余烬数组中的第一个对象

Javascript 重定向到余烬数组中的第一个对象,javascript,redirect,ember.js,Javascript,Redirect,Ember.js,我从中得到了一个部分解决方案,但它没有按预期工作: 我的路由器: App.Router.map(function() { this.resource('races', { path: '/'}, function() { this.resource('race', { path: '/:race_id'}, function() { this.route('edit'); this.route('delete');

我从中得到了一个部分解决方案,但它没有按预期工作:

我的路由器:

App.Router.map(function() {
    this.resource('races', { path: '/'}, function() {
        this.resource('race', { path: '/:race_id'}, function() {
            this.route('edit');
            this.route('delete');
            this.route('splits');
        });
        this.route('create');
        this.route('info');
    });
});
基本上,我有一个比赛列表,reach race有一个时间/速度视图(
race
资源)。我想要的是永远不要登陆
races
资源,因此如果我正在查看
race
并将其删除,我会被重定向到第一个
race

使用来自的解决方案,一切似乎都重定向到第一个
比赛
,此时我的所有链接和操作都中断,我“无法离开”第一个
比赛
路线

下面是我在另一篇文章中实现解决方案的方式:

App.RacesRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('race');
    },
    redirect: function(model) {
        var firstRace = this.modelFor('races').get('firstObject');
        this.transitionTo('race', firstRace);
    }
});

重定向
有两个参数传递给它,模型和转换对象。转换具有属性
targetName
,您可以根据其值有条件地重定向

redirect: function(model, transition){
  if(transition.targetName =='races.index'){
    this.transitionTo('race', model.get('firstObject'));
  }
}

明亮的工作起来很有魅力。谢谢你的简明解释!