@@ -2903,6 +2903,7 @@ var Container = Backgrid.Container = Backbone.View.extend({
2903
2903
2904
2904
this . height = options . height || 200 ;
2905
2905
this . throttle = options . throttle || this . throttle ;
2906
+ this . prefetch = "prefetch" in options ? options . prefetch : this . prefetch ;
2906
2907
this . grid = options . grid ;
2907
2908
this . onScroll = _ . debounce ( _ . bind ( this . onScroll , this ) , this . throttle ) ;
2908
2909
this . indicator = $ ( "<h1>HEY I'M LOADING OVER HERE</h1>" ) . hide ( ) ;
@@ -2912,27 +2913,46 @@ var Container = Backgrid.Container = Backbone.View.extend({
2912
2913
2913
2914
onScroll : function ( e ) {
2914
2915
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 ( ) ,
2917
2917
collection = this . grid . collection . pageableCollection ,
2918
- indicator = this . indicator ,
2918
+ hideIndicator = _ . bind ( this . hideIndicator , this ) ,
2919
+ prefetch = this . prefetch ,
2919
2920
next ;
2920
2921
2921
- if ( top >= maxScrollTop ) {
2922
- if ( next = collection . getNextPage ( {
2923
- scrolling : true
2924
- } ) ) {
2922
+ if ( shouldFetch ) {
2923
+ if ( next = collection . getNextPage ( { scrolling : true } ) ) {
2925
2924
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 ) ;
2932
2944
}
2933
2945
}
2934
2946
} ,
2935
2947
2948
+ showIndicator : function ( ) {
2949
+ this . indicator . detach ( ) . appendTo ( this . grid . footer . el ) ;
2950
+ } ,
2951
+
2952
+ hideIndicator : function ( ) {
2953
+ this . indicator . hide ( ) ;
2954
+ } ,
2955
+
2936
2956
onRequest : function ( collection , xhr , options ) {
2937
2957
if ( options . scrolling ) this . indicator . show ( ) ;
2938
2958
} ,
@@ -2942,7 +2962,8 @@ var Container = Backgrid.Container = Backbone.View.extend({
2942
2962
height : this . height + "px" ,
2943
2963
overflow : "scroll"
2944
2964
} ) ) ;
2945
- this . indicator . detach ( ) . appendTo ( this . grid . footer . el ) ;
2965
+
2966
+ this . hideIndicator ( ) ;
2946
2967
this . delegateEvents ( ) ;
2947
2968
return this ;
2948
2969
}
0 commit comments