|
1 |
| -// Backbone.PagedCollection.js 0.1.3 |
| 1 | +// Backbone.PagedCollection.js 0.1.4 |
2 | 2 |
|
3 | 3 | // (c) 2012 Amir Grozki
|
4 | 4 | // Distributed under the MIT license.
|
|
13 | 13 | this._reset = function() {
|
14 | 14 | _reset.call(this);
|
15 | 15 |
|
| 16 | + this.empty = true; |
16 | 17 | this.pages = {};
|
17 | 18 | };
|
18 | 19 |
|
|
27 | 28 | this.perPage = options.perPage || 10;
|
28 | 29 | this.total = options.total;
|
29 | 30 |
|
| 31 | + this.cacheFunction = options.cacheFunction || function(timestamp) { |
| 32 | + return false; |
| 33 | + }; |
| 34 | + |
30 | 35 | this.collection = options.collection || Backbone.Collection;
|
31 | 36 |
|
32 | 37 | this.url = this.collection.prototype.model.prototype.urlRoot;
|
|
35 | 40 |
|
36 | 41 | if (models) {
|
37 | 42 | 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; |
39 | 44 | }
|
40 | 45 | };
|
41 | 46 |
|
|
54 | 59 | success = options.success;
|
55 | 60 |
|
56 | 61 | if (!this.pages[ this.page ]
|
57 |
| - || this.pages[ this.page ].collection.length <= 0 |
| 62 | + || this.empty |
58 | 63 | || 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 )) { |
60 | 65 |
|
61 | 66 | this.trigger("fetching");
|
62 | 67 |
|
63 | 68 | collection = new this.collection();
|
64 | 69 | collection.url = this.url;
|
65 | 70 | 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 | + |
67 | 72 | options.success = _.bind(function(resp) {
|
68 | 73 |
|
69 | 74 | this.pages[ this.page ] = { timestamp: (new Date).getTime(), collection: collection };
|
70 | 75 |
|
| 76 | + this.empty = false; |
| 77 | + |
71 | 78 | //Backbone.Collection.prototype.reset.call(this, this.pages[ this.page ].collection.toArray() );
|
72 | 79 | this.trigger("reset");
|
73 | 80 |
|
|
79 | 86 |
|
80 | 87 | options.parse = this.parse;
|
81 | 88 | options.url = this.url() + '/page/' + this.page;
|
82 |
| - //options.data = filters; |
83 | 89 |
|
84 | 90 | if (this.dataFilter) {
|
85 | 91 | options.data = this.dataFilter;
|
|
116 | 122 | }
|
117 | 123 | }
|
118 | 124 |
|
| 125 | + if (this.total > 0) { |
| 126 | + this.empty = false; |
| 127 | + } |
| 128 | + |
119 | 129 | if (pageCount < this.page) {
|
120 | 130 | this.page = pageCount;
|
121 | 131 | }
|
|
0 commit comments