Trunk JS Pagination

I tried the JS trunk with rails and really love how it organizes the interface. I implemented a pagination solution similar to the one presented here https://gist.github.com/705733

I'm just wondering what the role of the collection is and how it should work with paginated results. Currently, it seems that when I retrieve new objects from the database, they override what is in the current collection. However, I could use {add: true} to add to the current collection. This would make pagination difficult. What about caching the result? Should I create a new collection for each page?

If someone has done this or knows how to do it, it will be very helpful.

+5
source share
1 answer

If your goal is to request and display elements when requesting a page, you can 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();
 }     
}

This way you save a collection for every page.

If you also need a collection of all the items so far selected, just save one - and add the downloaded items to it every time you get them from the server.

+11
source

All Articles