Skip to content

Commit 07ef13b

Browse files
committed
🔔 checkLastPage for path: function
1 parent 0fea4e6 commit 07ef13b

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

‎js/page-load.js‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,24 +167,40 @@ proto.onAppendOutlayer = function( response, path, items ) {
167167
// check response for next element
168168
proto.checkLastPage = function( response, path ) {
169169
var checkLastPage = this.options.checkLastPage;
170+
if ( !checkLastPage ) {
171+
return;
172+
}
173+
174+
var pathOpt = this.options.path;
175+
// if path is function, check if next path is truthy
176+
if ( typeof pathOpt == 'function' ) {
177+
var nextPath = this.getPath();
178+
if ( !nextPath ) {
179+
this.lastPageReached( response, path );
180+
return;
181+
}
182+
}
170183
// get selector from checkLastPage or path option
171184
var selector;
172185
if ( typeof checkLastPage == 'string' ) {
173186
selector = checkLastPage;
174187
} else if ( this.isPathSelector ) {
175-
selector = this.options.path;
188+
// path option is selector string
189+
selector = pathOpt;
176190
}
177-
// bail if checkLastPage disabled or no selector or not document response
178-
if ( !checkLastPage || !selector || !response.querySelector ) {
191+
// check last page for selector
192+
// bail if no selector or not document response
193+
if ( !selector || !response.querySelector ) {
179194
return;
180195
}
181196
// check if response has selector
182197
var nextElem = response.querySelector( selector );
183-
if ( nextElem ) {
184-
// page has selector element, keep going
185-
return;
198+
if ( !nextElem ) {
199+
this.lastPageReached( response, path );
186200
}
187-
// no next selector, last page hit
201+
};
202+
203+
proto.lastPageReached = function( response, path ) {
188204
this.canLoad = false;
189205
this.dispatchEvent( 'last', null, [ response, path ] );
190206
};

‎test/unit/check-last-page.js‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ QUnit.test( 'checkLastPage', function( assert ) {
4646
scrollThreshold: false,
4747
history: false,
4848
});
49-
49+
5050
infScroll.on( 'last', onStringLast1 );
5151
infScroll.once( 'append', onStringAppend1 );
5252
infScroll.loadNextPage();
@@ -66,6 +66,47 @@ QUnit.test( 'checkLastPage', function( assert ) {
6666
function loadStringPage3() {
6767
infScroll.once( 'last', function() {
6868
assert.ok( true, 'checkLastPage: \'string\', last triggered on 3rd page' );
69+
setTimeout( checkPathFunction );
70+
});
71+
72+
infScroll.loadNextPage();
73+
}
74+
75+
// ----- path: function ----- //
76+
77+
function checkPathFunction() {
78+
infScroll.destroy();
79+
infScroll = new InfiniteScroll( '.demo--check-last-page', {
80+
// provide only page/2.html, then falsy
81+
path: function() {
82+
if ( this.loadCount === 0 ) {
83+
var nextIndex = this.loadCount + 2;
84+
return 'page/' + nextIndex + '.html';
85+
}
86+
},
87+
checkLastPage: true,
88+
append: '.post',
89+
scrollThreshold: false,
90+
history: false,
91+
});
92+
93+
infScroll.on( 'last', onFunctionLast2 );
94+
infScroll.once( 'append', onFunctionAppend2 );
95+
96+
infScroll.loadNextPage();
97+
}
98+
99+
function onFunctionLast2() {
100+
assert.ok( false, 'last should not trigger on function page 2' );
101+
}
102+
103+
function onFunctionAppend2() {
104+
infScroll.off( 'last', onFunctionLast2 );
105+
106+
infScroll.on( 'last', function( response, path ) {
107+
assert.ok( true, 'path: function, last triggered' );
108+
assert.ok( response, 'path: function, response there on last' );
109+
assert.ok( path, 'path: function, path there on last' );
69110
done();
70111
});
71112

0 commit comments

Comments
 (0)