use*_*853 7 backbone.js underscore.js
如何在backbone.js中链接收集方法?
var Collection = this.collection;
Collection = Collection.where({county: selected});
Collection = Collection.groupBy(function(city) {
return city.get('city')
});
Collection.each(function(city) {
// each items
});
Run Code Online (Sandbox Code Playgroud)
我试过这样的事,但这是错的:
Object[object Object],[object Object],[object Object] has no method 'groupBy'
Run Code Online (Sandbox Code Playgroud)
Cla*_*jda 14
你无法以Backbone.Collection这种方式访问方法(希望我没有错)但是你可能知道大多数Backbone方法都是基于Underscore.js的方法,所以这意味着如果你查看where方法的源代码,你会发现它使用的是Underscore. js filter方法,这意味着你可以实现你想做的事情:
var filteredResults = this.collection.chain()
.filter(function(model) { return model.get('county') == yourCounty; })
.groupBy(function(model) { return model.get('city') })
.each(function(model) { console.log(model); })
.value();
Run Code Online (Sandbox Code Playgroud)
.value()这里没有任何使用你的,你这是"东西"里面.each的方法对每个型号的,但如果你想假设返回过滤城市的数组,你可以用做.map和filteredResults将是你的结果
var filteredResults = this.collection.chain()
.filter(function(model) { return model.get('county') == yourCounty; })
.map(function(model) { return model.get('city'); })
.value();
console.log(filteredResults);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5454 次 |
| 最近记录: |