Skip to content
This repository was archived by the owner on Apr 2, 2019. It is now read-only.

Commit 5c03d6b

Browse files
committed
Added prefetching
1 parent 834515d commit 5c03d6b

File tree

3 files changed

+71
-29
lines changed

3 files changed

+71
-29
lines changed

lib/backgrid.js

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2903,6 +2903,7 @@ var Container = Backgrid.Container = Backbone.View.extend({
29032903

29042904
this.height = options.height || 200;
29052905
this.throttle = options.throttle || this.throttle;
2906+
this.prefetch = "prefetch" in options ? options.prefetch : this.prefetch;
29062907
this.grid = options.grid;
29072908
this.onScroll = _.debounce(_.bind(this.onScroll, this), this.throttle);
29082909
this.indicator = $("<h1>HEY I'M LOADING OVER HERE</h1>").hide();
@@ -2912,27 +2913,46 @@ var Container = Backgrid.Container = Backbone.View.extend({
29122913

29132914
onScroll: function (e) {
29142915
var top = e.target.scrollTop,
2915-
prefetch = this.prefetch,
2916-
maxScrollTop = e.target.scrollHeight - this.$el.height(),
2916+
shouldFetch = top >= e.target.scrollHeight - this.$el.height(),
29172917
collection = this.grid.collection.pageableCollection,
2918-
indicator = this.indicator,
2918+
hideIndicator = _.bind(this.hideIndicator, this),
2919+
prefetch = this.prefetch,
29192920
next;
29202921

2921-
if (top >= maxScrollTop) {
2922-
if (next = collection.getNextPage({
2923-
scrolling: true
2924-
})) {
2922+
if (shouldFetch) {
2923+
if (next = collection.getNextPage({ scrolling: true })) {
29252924
next.then(function() {
2926-
prefetch && collection.hasNext() && collection.getNextPage({
2927-
scrolling: true
2928-
});
2929-
}).always(function() {
2930-
indicator.hide();
2931-
});
2925+
if (!prefetch) return;
2926+
2927+
// Prefetching sequence
2928+
(function prefetchNext(remaining) {
2929+
if (!remaining) return;
2930+
2931+
console.info("prefetching, remaining ", remaining);
2932+
2933+
if (collection.hasNext()) {
2934+
collection.getNextPage({
2935+
scrolling: true
2936+
}).then(function() {
2937+
console.info("prefetched ", remaining, "decreasing remaining");
2938+
return --remaining;
2939+
}).then(prefetchNext).always(hideIndicator);
2940+
}
2941+
})(prefetch);
2942+
2943+
}).always(hideIndicator);
29322944
}
29332945
}
29342946
},
29352947

2948+
showIndicator: function() {
2949+
this.indicator.detach().appendTo(this.grid.footer.el);
2950+
},
2951+
2952+
hideIndicator: function() {
2953+
this.indicator.hide();
2954+
},
2955+
29362956
onRequest: function (collection, xhr, options) {
29372957
if (options.scrolling) this.indicator.show();
29382958
},
@@ -2942,7 +2962,8 @@ var Container = Backgrid.Container = Backbone.View.extend({
29422962
height: this.height + "px",
29432963
overflow: "scroll"
29442964
}));
2945-
this.indicator.detach().appendTo(this.grid.footer.el);
2965+
2966+
this.hideIndicator();
29462967
this.delegateEvents();
29472968
return this;
29482969
}

0 commit comments

Comments
 (0)