【问题标题】:Ember Routes How To?Ember 路线如何?
【发布时间】:2015-04-09 03:52:00
【问题描述】:

我的问题很“简单”,但我无法用 Ember 解决它......

这是一个小型图书馆应用程序,作者和书籍在这些路线上运行良好

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

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

现在我想添加一条路线,允许我使用来自当前作者模板/authors/156的链接注册新书

路由必须打开一个books/new模板,并将new book对象与他的author链接:即我要显示<h1>New book from {{author.name}}</h1>

我应该在现有路线上添加什么路线? 如何将作者引用传递给新书对象?

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    我看到了三种方法:

    1. 放在books资源下,并要求作者作为路由参数:

      this.resource('books', function() {
          this.route('new', { path: '/new/:author_id' });
      });
      
    2. 将路由放在books 资源下,但将作者放在query parameter 中。

      this.resource('books', function() {
          // Declare required query parameter on controller for `new` route
          this.route('new');
      });
      
    3. 将路由放在authors下,并在URL中要求作者:

      this.resource('authors', function() {
          this.route('new_book', { path: '/:author_id/new_book' });
      });
      

    我会建议第三种选择,因为我认为它是最干净的。在您的控制器中,您可以相当轻松地创建一本新书:

    export default Ember.Controller.extend({
        actions: {
            createBook: function() {
                var author = this.get('model');
                var book = this.store.createRecord('book', {
                    author: author,
                    ...
                });
    
                book.save();
            }
        }
    });
    

    【讨论】:

    • 首先,感谢您的回答。也许我错了,但我不喜欢创建author.newbook route 的想法。我更喜欢book.new 一个。我正在尝试第一个选项book/new/:author_id,但我现在遇到了另一个问题:如何将新书链接到作者。我认为它必须在BookNewRoutemodel: function(){return this.store.createRecord('book')} 内完成,但如何?
    【解决方案2】:

    我已经尝试了第二种建议方法,查询参数,并取得了成功。

    路由器:

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

    路线

    App.BooksNewRoute = Ember.Route.extend({
        queryParams: {
            author_id: {
                refreshModel: true
            }
        },
        model: function (params) {
            var newBook = this.store.createRecord('book');
            this.store.find('author', params.author_id).then(function (author) {
                newBook.set('author', author);
            });
            return newBook;
        }
    });
    

    和控制器

    App.BooksNewController = Ember.ObjectController.extend({
        queryParams: ['author_id'],
        author_id: null,
        actions: {
            save: function () {
                var controller = this;
                this.get('model').save().then(function (book) {
                    controller.transitionToRoute('books.show', book);
                }, function (error) {
                    console.error(error);
                });
            },
            cancel: function () {
                this.get('model').rollback();
                this.transitionToRoute('index');
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2014-03-04
      • 1970-01-01
      • 2017-07-18
      • 2014-04-18
      • 2014-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多