javascript - Ember.js 如何扁平化异步 hasMany 关系

标签 javascript ember.js

就说我有这样的关系:

App.Library = DS.Model.extend
    authors = DS.hasMany(App.Author, {async: true})

App.Author = DS.Model.extend
    books: DS.hasMany(App.Book, {async: true)

App.Book = DS.Model.extend()

我想从我图书馆的所有作者那里得到所有的书。如果这些不是Promises

,我可以做一个简单的扁平化操作

当两个关系都是异步时,我如何做类似的事情。

这是我想出来的

allBooks: Ember.computed('authors', function() {
      var r = []
      this.get('authors').then(function(authors) {
          authors.forEach(function(author) {                  
              author.get('books').then(function(books) {
                  books.forEach(function(book){
                      r.push.apply(r, book);
                  });
              });
            });
      });

    return r;
}

更新:

我应该早点说明这一点,但实际上我确实使用了以下代码。但现在 ArrayController 已被弃用,我正在寻找其他方法来实现同样的结果。

 allBooks: Ember.computed('authors', function() {
      var allBooks  = Ember.ArrayController.create();
      this.get('authors').then(function(authors) {
          authors.forEach(function(author) {                  
              author.get('books').then(function(books) {
                  books.forEach(function(book){
                      allBooks.addObject(book);
                  });
              });
            });
      });

    return allBooks;
}

我想我可以做这样的事情还有其他更好的方法吗?:

books: Ember.computed('authors', function() {
      var a  = Ember.Object.create({
          content: Ember.A()
      });
      this.get('authors').then(function(authors) {
          authors.forEach(function(author) {
              author.get('books').then(function(books) {
                  books.forEach(function(book){
                      a.get('content').addObject(book);
                  });
              });
            });
      });

    return a.get('content');
}),

最佳答案

你可以在你的 route 组装数组,使用PromiseProxyMixin在你的 Controller 中(或者如果你不希望它们作为主模型,则附加到你的 Controller 的其他一些对象)。

// in route
setupController: function(controller, model) {
    var allBooksPromise = new Ember.RSVP.Promise(function (resolve, reject) {
        // get your books here
        var allBooks = ...
        resolve(allBooks);
    }
    controller.set('promise', allBooksPromise);
}

// in controller
export default Ember.Controller.extend(Ember.PromiseProxyMixin);

// in template
{{#if controller.isPending}}
    Getting all of the books...
{{else}}
    <ul>
    {{#each controller.content as |book|}}
       <li>{{book.title}}</li>
    {{/each}}
    </ul>
{{/if}}

关于javascript - Ember.js 如何扁平化异步 hasMany 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31445417/

相关文章:

ember.js - 在Ember.js中从belongsTo关联中查找记录

Javascript 嵌套函数丢失作用域

javascript - 是否可以为同一个 key 提取两次对象 key ?

javascript - 可视化节点树(斥力?)

javascript - 响应.on 是什么意思? Node.js

javascript - ember-cli 中没有单例的依赖注入(inject)

sproutcore - 使用Sproutcore 2将参数从 View 传递到 Controller

twitter-bootstrap - 使用 ember.js 模板 Bootstrap 弹出框

javascript - 有没有办法用假计时器运行 Ember.Testing 验收测试?

javascript - 悬停时为气泡图中的气泡设置的白色边框/线条颜色不会在鼠标从气泡中移出时重置