Ember检索并将记录链接到新记录

时间:2015-04-08 15:17:18

标签: ember.js

创建一个简单的图书馆应用程序,包括作者和书籍。书籍和作者之间存在关系(belongsTo / hasMany)

我正在尝试使用this.route('new', { path: '/new/:author_id' });

创建新书

我的路线是:

App.BooksNewRoute = Ember.Route.extend({
    model: function (params) {
        return this.store.createRecord('book');
    }
});

我知道我可以使用params.author_id检索author_id但是..

如何将model.author绑定到author_id 标识的作者记录

由于

1 个答案:

答案 0 :(得分:0)

您是否考虑过这些路线?

  this.resource('author', function () {
    this.route('show', {path: '/:author_id'}, function () {
      this.resource('book', function() {
        this.route('new');
      });
    });
  });

在这种情况下,作者可以作为author/show路线的模型......:

this.store.createRecord('book', {author: this.modelFor('author/show')});

如果您还没有作者,则必须从商店获取...:

this.store.find('author', id).then(
  function(author){
    return this.store.createRecord('book', {author: author});
  }
)

(警告:代码未经测试,但应该显示想法)

相关问题