Skip to content

Commit 0071b1e

Browse files
committed
🐞 stop prefilling on last or error
πŸ›  checkLastPage on append βœ… add test for prefilling on error Ref #690
1 parent ae3dd2c commit 0071b1e

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

β€Žjs/page-load.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ InfiniteScroll.defaults.responseType = 'document';
3737
InfiniteScroll.create.pageLoad = function() {
3838
this.canLoad = true;
3939
this.on( 'scrollThreshold', this.onScrollThresholdLoad );
40+
this.on( 'append', this.checkLastPage );
4041
if ( this.options.outlayer ) {
4142
this.on( 'append', this.onAppendOutlayer );
4243
}
@@ -94,7 +95,6 @@ proto.appendNextPage = function( response, path ) {
9495
this.appendItems( items, fragment );
9596
this.isLoading = false;
9697
this.dispatchEvent( 'append', null, [ response, path, items ] );
97-
this.checkLastPage( response, path );
9898
}.bind( this );
9999

100100
// TODO add hook for option to trigger appendReady
@@ -229,6 +229,8 @@ InfiniteScroll.create.prefill = function() {
229229
this.updateScroller();
230230
this.isPrefilling = true;
231231
this.on( 'append', this.prefill );
232+
this.once( 'error', this.stopPrefill );
233+
this.once( 'last', this.stopPrefill );
232234
this.prefill();
233235
};
234236

@@ -239,7 +241,7 @@ proto.prefill = function() {
239241
this.log('prefill');
240242
this.loadNextPage();
241243
} else {
242-
this.off( 'append', this.prefill );
244+
this.stopPrefill();
243245
}
244246
};
245247

@@ -252,6 +254,11 @@ proto.getPrefillDistance = function() {
252254
return this.windowHeight - this.element.clientHeight;
253255
};
254256

257+
proto.stopPrefill = function() {
258+
console.log('stopping prefill');
259+
this.off( 'append', this.prefill );
260+
};
261+
255262
// -------------------------- request -------------------------- //
256263

257264
function request( url, responseType, onLoad, onError ) {

β€Žtest/index.html

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<script src="unit/check-last-page.js"></script>
3636
<script src="unit/prefill-window.js"></script>
3737
<script src="unit/prefill-element.js"></script>
38+
<script src="unit/prefill-element-last.js"></script>
3839
<script src="unit/scroll-watch-window.js"></script>
3940
<script src="unit/scroll-watch-element.js"></script>
4041
<script src="unit/history-window.js"></script>
@@ -87,6 +88,10 @@ <h2>prefill, element</h2>
8788
<div class="demo demo--prefill-element demo--big-posts">
8889
</div>
8990

91+
<h2>prefill, element, last</h2>
92+
<div class="demo demo--prefill-element-last demo--big-posts">
93+
</div>
94+
9095
<h2>scroll watch window</h2>
9196
<div class="demo demo--scroll-watch-window"></div>
9297

β€Žtest/unit/check-last-page.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ QUnit.test( 'checkLastPage', function( assert ) {
4141
infScroll.destroy();
4242
infScroll = new InfiniteScroll( '.demo--check-last-page', {
4343
path: 'page/{{#}}.html',
44-
checkLastPage: '.next-page-link',
44+
checkLastPage: '.check-last-page-next-link',
4545
append: '.post',
4646
scrollThreshold: false,
4747
history: false,
@@ -79,8 +79,8 @@ QUnit.test( 'checkLastPage', function( assert ) {
7979
infScroll = new InfiniteScroll( '.demo--check-last-page', {
8080
// provide only page/2.html, then falsy
8181
path: function() {
82-
if ( this.loadCount === 0 ) {
83-
var nextIndex = this.loadCount + 2;
82+
if ( this.pageIndex < 3 ) {
83+
var nextIndex = this.pageIndex + 1;
8484
return 'page/' + nextIndex + '.html';
8585
}
8686
},

β€Žtest/unit/prefill-element-last.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
QUnit.test( 'prefill element last', function( assert ) {
2+
3+
// expected load count, each post is 200px tall
4+
var expLoadCount = 2;
5+
6+
var done = assert.async( expLoadCount );
7+
8+
var infScroll = new InfiniteScroll( '.demo--prefill-element-last', {
9+
path: function() {
10+
return this.loadCount < 2 ? 'page/prefill.html' : false;
11+
},
12+
append: '.post',
13+
prefill: true,
14+
elementScroll: true,
15+
history: false,
16+
scrollThreshold: false,
17+
debug: true,
18+
onInit: function() {
19+
this.on( 'append', onAppend );
20+
this.on( 'error', onError );
21+
},
22+
});
23+
24+
function onAppend() {
25+
assert.ok( true, 'prefill element appended post ' + infScroll.loadCount );
26+
if ( infScroll.loadCount == expLoadCount ) {
27+
assert.equal( infScroll.loadCount, expLoadCount,
28+
expLoadCount + ' pages appended' );
29+
}
30+
done();
31+
}
32+
33+
function onError() {
34+
assert.ok( false, 'error should not trigger' );
35+
}
36+
37+
});

0 commit comments

Comments
Β (0)