Backbone.js 中的分页 [英] Pagination in Backbone.js
问题描述
我知道有一个用于此的组件,但根据我所看到的,您必须创建一个扩展组件的新集合.有没有其他方法可以在主干中进行分页?
I know that there is a component for this but based on what i see you have to create a new collection with the component extended. Is there another way to do pagination in backbone?
我只需要一个上一个和下一个按钮将每页的项目限制为 12.我一直在 javascript 上创建它(对于生产环境来说不是一个好的解决方案).有什么想法吗?
All i need is just a previous and next button limit the items per page to 12. i've been creating it on javascript ( not a good solution for production environment ). Any ideas?
推荐答案
由于 Backbone 集合扩展了下划线方法,您可能希望非常容易地创建辅助分页方法.我使用类似的东西:
Since Backbone collection has underscore methods extended, you might want to create helper pagination method very easy. I use something like :
var Paginated = Backbone.Collection.extend({
pagination : function(perPage, page) {
page = page-1;
var collection = this;
collection = _(collection.rest(perPage*page));
collection = _(collection.first(perPage));
return collection.map( function(model) { return model.toJSON() } );
}
});
这将返回您收藏的JSON,您可以在jsfiddle中使用它:http://jsfiddle.net/YHmrp/2/
This returns toJSON of your collection, you may play with it in the jsfiddle : http://jsfiddle.net/YHmrp/2/
这篇关于Backbone.js 中的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!