何时过滤集合与在Backbone中使用多个集合是否合适?
例如,考虑一个音乐库应用程序.它将具有显示流派的视图和用于显示所选流派的音乐的另一视图.
你宁愿用所有音乐制作一个巨大的集合,然后过滤它或几个较小的音乐?
只有一个集合允许你添加其他属性进行过滤的功能,但是假设你有大量的音乐:如果用户只需要1种类型,你如何防止在应用程序启动时加载它们?
我认为最简单的方法是拥有一个共同的独特集合,该集合fetch已智能地从服务器中按流派数据进行过滤:
// code simplified and no tested
var SongsCollection = Backbone.Collection.extend({
model: Song,
url: function() {
return '/songs/' + this.genre;
},
initialize: function( opts ){
this.genre = opts.genre;
}
});
var mySongsCollection = new SongsCollection({ genre: "rock" });
mySongsCollection.fetch();
Run Code Online (Sandbox Code Playgroud)
您必须使此集合在用户更改所选流派时从服务器重新获取数据:
mySongsCollection.genre = "punk";
mySongsCollection.fetch();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
645 次 |
| 最近记录: |