过滤一个骨干集合返回模型的数组 [英] Filtering a Backbone Collection returns an array of Models
问题描述
样code:
this.books = this.getBooksFromDatabase();
this.publishedBooks = this.books.filter(function(book) {
return book.get("isPublished") === "1";
});
下面就在于这个问题:
this.books.filter,返回模型的阵列。我已经试过包装的阵列,因为这样:
this.books.filter, returns an array of the models. I've tried wrapping the array, as such:
var publishedBooks = _( this.books.filter(function(book) {
return book.get("isPublished") === "1";
}))
所推荐的这篇文章:
<一href=\"https://github.com/documentcloud/backbone/issues/120\">https://github.com/documentcloud/backbone/issues/120
但我仍然无法运行之类的东西:
publishedBooks.each(...),或
publishedBooks.get(...)
But i still can't run things like: publishedBooks.each(...), or publishedBooks.get(...)
我在想什么?有没有一种方法,以返回数组转换成集合?
What am I missing? Is there a way to convert the returned array into a collection?
推荐答案
您既可以实例化一个新的骨干收集和传递数组中为止。
You could either instantiate a new backbone collection and pass in the array.
var myPublishedBooks = new MyBooksCollection(publishedBooks);
或者你可以刷新你的原始集合。
Or you could refresh your original collection.
this.books.refresh(publishedBooks)
注意的 0.5.0版本于2011年7月更名为的刷新
到重置
,这样你就可以在主干的较新版本实现这一点;
Note that the 0.5.0 release in July 2011 renamed refresh
to reset
, so you can achieve this in newer versions of Backbone with;
this.books.reset(publishedBooks)
这篇关于过滤一个骨干集合返回模型的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!