Ember.js 余烬-路由/嵌套/参数/HasMany

Ember.js 余烬-路由/嵌套/参数/HasMany,ember.js,ember-data,Ember.js,Ember Data,我仍在学习余烬,我想得到一些帮助来解决这个问题 我有一个简单的图书馆应用程序,里面有作者和书籍。 我想向我的“作者”视图模板添加“添加书籍”链接 我希望这个链接能够呈现一个新的图书模板,并定义book.author。但我想把它展示在主插座上 我的模型: App.Author = DS.Model.extend({ name: DS.attr('string'), about: DS.attr('string'), picture: DS.attr('string'),

我仍在学习余烬,我想得到一些帮助来解决这个问题

我有一个简单的图书馆应用程序,里面有作者和书籍。 我想向我的“作者”视图模板添加“添加书籍”链接 我希望这个链接能够呈现一个新的图书模板,并定义book.author。但我想把它展示在主插座上

我的模型:

App.Author = DS.Model.extend({
    name: DS.attr('string'),
    about: DS.attr('string'),
    picture: DS.attr('string'),
    books: DS.hasMany('book', {async: true})
});

App.Book = DS.Model.extend({
    title: DS.attr('string'),
    isbn: DS.attr('string'),
    summary: DS.attr('string'),
    isAvailable: DS.attr('boolean'),
    featured: DS.attr('boolean'),
    picture: DS.attr('string'),
    author: DS.belongsTo('author', {async: true})
});
以下是我的路线:

App.Router.map(function () {

    this.resource('books', function () {
        this.route('edit', {path: '/:book_id/edit'});
        this.route('view', {path: '/:book_id'});
    });

    this.resource('authors', function () {
        this.route('new');
        this.route('edit', {path: '/:author_id/edit'});
        this.route('view', {path: '/:author_id'}, function (){
            this.resource('books', function(){
                this.route('new');
            });
        });
    });
});
URL将类似于authors/4/books/new

使用此嵌套路由,我只能在authors outlet中显示我的books.new模板。我不能在主插座上显示它

我认为另一种方式是使用这些路线

App.Router.map(function () {

    this.resource('books', function () {
        this.route('new');
        this.route('edit', {path: '/:book_id/edit'});
        this.route('view', {path: '/:book_id'});
    });

    this.resource('authors', function () {
        this.route('new');
        this.route('edit', {path: '/:author_id/edit'});
        this.route('view', {path: '/:author_id'});
    });
});
以及使用QueryParams

URL将类似于书籍/新?作者id=4

模板显示正确,但我无法将我的新书记录绑定到其作者

什么是余烬的最佳实践,使其工作?你能给我一个工作的例子吗


谢谢。

您的嵌套路线方法很好,然后在嵌套手册/新控制器中添加:

needs: ['authors/view'],
author: Ember.computed.alias('controllers.authors.view.model')
然后假设您的书本/新控制器上有“保存”操作:

save: function() {
  var model = this.get('model');
  var author = this.get('author');
  model.set('author', author);
  model.save().then(...);
}

我可能会将author.view更改为author.show,以避免与余烬视图混淆。我无法解决嵌套路由问题:问题中显示的路由无法与索引路由´\´;未捕获的TypeError:无法读取未定义的属性“shouldSupercered”让我抓狂!您试图在索引路由中链接到什么?我猜这是一条带有动态段的路线,您没有提供要链接到的对象。例如,{{链接到“作者”作者。编辑}}应该是:{{链接到“作者”作者。编辑“某些作者”{u对象}