骨干JS分页 [英] Backbone JS Pagination
问题描述
我一直在尝试骨干JS用钢轨和真的很喜欢它的方式组织的前端。我已经实施了类似这里的一个分页解决方案 https://gist.github.com/705733
I have been trying out backbone JS with rails and really like the way it organises the frontend. I have implemented a pagination solution similar to the one here https://gist.github.com/705733
我只是想知道一个集合的角色是什么,以及如何它应该分页结果工作。目前看来,当我从数据库中获取新的对象,他们忽略什么是当前的收藏。不过,我可以使用{补充:真}添加到当前集合。那么这将使得分页更加困难。又有怎样缓存结果?我应该为每个页面创建一个新的集合?
I'm just wondering what the role of a collection is and how it should work with paginated results. Currently, it seems that when I fetch new objects from the database they override what's in the current collection. However, I could use {add: true} to add to current collection. This would then make pagination more difficult. And how about caching the result? Should i create a new collection for every page?
如果有人已经这样做了或者知道如何去那将是很大的帮助。
If anyone has done this or knows how to go about it would be of great help.
推荐答案
如果你的目标是查询和放大器;显示项目当请求一个页面,你可以不喜欢(伪code):
If your objective is to query & display items when a page is requested, you could do something like (pseudocode):
pages = {}
// when page N is needed
function fetch_page(n) {
if (!pages[n]) {
pages[n] = new ItemsCollection({page: n})
pages[n].fetch();
}
}
所以,你保持一个集合的每一页。
So you keep a collection for each page.
如果您还需要所有项目的集合一次读取到目前为止,只保留一个 - 每次你从服务器得到他们一次添加物品取出它
If you also need a collection of all the items fetched so far, just keep one - and add fetched items to it every time you get them from the server.
这篇关于骨干JS分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!