在 Emberjs 中创建后转到新项目路线

Go to new item route after creation in Emberjs

我的路线中有这样的东西:

actions: {
  save(title, description) {
    const newCard = this.get('store').createRecord('card', { title, description } );
    newCard.save().then((card) => {
      // card.id contains the id of the new card
      this.transitionTo('cards.all'); // problem with this line
    });
  }
}

创建后我想去新的卡片路线。我可以从 card.id 得到新项目的 id。我尝试了以下但没有用:

this.transitionTo('cards.card', card.id);

这会引发一些错误,因为 cards.card 路由无法在参数中找到 id

以下:

this.transitionTo('cards/' + card.id);

抛出一些错误,指出 cards/45 路线未找到,但如果创建了新项目并且我可以导航到它。

我正在使用 Ember 2.6.1.

编辑

我的 router.js 文件:

this.route('cards', function() {
  this.route('all');
  this.route('card', {path: '/:card_id'}, function() {
    this.route('edit');
  });
  this.route('new');
});

这是您的路由器当前的样子吗?

this.route('cards', function() {
    this.route('all');
    this.route('card', {path: '/:card_id'}, function() {
    this.route('edit');
  });
  this.route('new');
});

如果您想为特定卡片转到新路线,那么您需要为新卡片定义一个 slug。像

this.route('new', {path: /:card_id});

你的动作散列中的过渡像这样:

this.transitionTo('new', card.id);

希望我正确理解了您的用例。

应该是这样的:

this.transitionTo('cards.card', card);

我只需要这样做,因为我的 card.js 需要 params 对象并且我试图传递一个数字:

model(params) {
  return this.store.find('card', params.id);
}