Skip to content
This repository was archived by the owner on Oct 8, 2022. It is now read-only.

Commit f8c1e35

Browse files
committed
Upgraded to 0.1.4, with an addition of a cache function. See README.
1 parent 10da688 commit f8c1e35

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ A Backbone collection with paging and simple caching capabilities
1010
PagedCollection accepts the same options as Backbone.Collection, with the following additions:
1111
***perPage***: number of items to display per page, defaults to `10`.
1212
***collection***: type of collection to use as the page collection, defaults to `Backbone.Collection`.
13+
***cacheFunction***: a function which accepts a timestamp and should return true if the page should be re-fetched, or false otherwise.
1314

1415
There's an optional `collection.filter()` function, which accepts an object of key-values, resets the collection and passes the filter object as the data in the `Backbone.sync` requests.
1516

17+
The `fetch()` function may also accept the same options as with a normal collection, with the following addition:
18+
***force***: when `true`, will fetch the current page from the server without regard to it being cached.
19+
1620
## Server-side Integration ##
1721

1822
PagedCollection expects server responses to be of the following format:

backbone.pagedcollection.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Backbone.PagedCollection.js 0.1.3
1+
// Backbone.PagedCollection.js 0.1.4
22

33
// (c) 2012 Amir Grozki
44
// Distributed under the MIT license.
@@ -13,6 +13,7 @@
1313
this._reset = function() {
1414
_reset.call(this);
1515

16+
this.empty = true;
1617
this.pages = {};
1718
};
1819

@@ -27,6 +28,10 @@
2728
this.perPage = options.perPage || 10;
2829
this.total = options.total;
2930

31+
this.cacheFunction = options.cacheFunction || function(timestamp) {
32+
return false;
33+
};
34+
3035
this.collection = options.collection || Backbone.Collection;
3136

3237
this.url = this.collection.prototype.model.prototype.urlRoot;
@@ -35,7 +40,7 @@
3540

3641
if (models) {
3742
this.reset(models, { silent: true, parse: options.parse, total: this.total });
38-
this.total = options.total || models.length
43+
this.total = options.total || models.length;
3944
}
4045
};
4146

@@ -54,20 +59,22 @@
5459
success = options.success;
5560

5661
if (!this.pages[ this.page ]
57-
|| this.pages[ this.page ].collection.length <= 0
62+
|| this.empty
5863
|| options.force // Allow a forced fetch for manual update.
59-
/*|| this.pages[ this.page ].timestamp < blabla // Something with the cache timestamp? */) {
64+
|| this.cacheFunction( this.pages[ this.page ].timestamp )) {
6065

6166
this.trigger("fetching");
6267

6368
collection = new this.collection();
6469
collection.url = this.url;
6570
collection.parse = this.parse;
66-
this.pages[ this.page ] = { timestamp: (new Date).getTime(), collection: collection }; // added this line cause if u use any of eech etc methods pages[this.page] wont be defined yet
71+
6772
options.success = _.bind(function(resp) {
6873

6974
this.pages[ this.page ] = { timestamp: (new Date).getTime(), collection: collection };
7075

76+
this.empty = false;
77+
7178
//Backbone.Collection.prototype.reset.call(this, this.pages[ this.page ].collection.toArray() );
7279
this.trigger("reset");
7380

@@ -79,7 +86,6 @@
7986

8087
options.parse = this.parse;
8188
options.url = this.url() + '/page/' + this.page;
82-
//options.data = filters;
8389

8490
if (this.dataFilter) {
8591
options.data = this.dataFilter;
@@ -116,6 +122,10 @@
116122
}
117123
}
118124

125+
if (this.total > 0) {
126+
this.empty = false;
127+
}
128+
119129
if (pageCount < this.page) {
120130
this.page = pageCount;
121131
}

0 commit comments

Comments
 (0)