Ember.js如何扁平化async的许多关系
Ember.js how to flatten async hasMany relationships
假设我有一个这样的关系:
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');
}),
你可以在你的路由中组装数组,在你的控制器中使用PromiseProxyMixin(或者其他一些附加到你的控制器的对象,如果你不想让它们作为主模型)。
// 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}}
相关文章:
- 气质的“nestRemoting()”有时可以'找不到关系
- 如何在Parse中创建一对多关系
- 为什么在许多浏览器中drawImage()的性能略好于createPattern()
- Ext.js从json构建模型关系的问题
- JSDoc:模块和名称空间之间的关系是什么
- 单元测试依赖关系没有被嘲笑
- 在Nodejs中为许多用户处理计时器
- FRP 中 EventStreams 的循环依赖关系
- 有许多关系创建记录 - 为什么未定义
- Ember js - 更新其他表后,有许多关系中断
- 余烬.js有许多模型关系保存
- 对于具有一对多关系的主干模型,存储“;许多“;数组中的模型,而不是Collection
- Ember排序有许多关系
- 余烬装置和有许多关系
- 序列自引用有许多关系
- Ember.js如何扁平化async的许多关系
- 如何在父模型中添加新的许多关系
- Ember数据有许多关系空结果
- 在车把模板中显示具有许多余烬关系的第一项
- 如何在javascript中动态创建具有许多父级和子级关系的Json