Skip to content

Commit 4ea72ba

Browse files
authored
Merge pull request #310 from fieg/fix-none-left-single-page
Fire noneLeft on single page scenario
2 parents 181edf2 + e421553 commit 4ea72ba

File tree

4 files changed

+103
-4
lines changed

4 files changed

+103
-4
lines changed

src/jquery-ias.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,12 @@
388388

389389
this.nextUrl = this.getNextUrl();
390390

391+
if (!this.nextUrl) {
392+
this.fire('noneLeft', [this.getLastItem()]);
393+
}
394+
391395
// start loading next page if content is shorter than page fold
392-
if (currentScrollOffset >= scrollThreshold) {
396+
if (this.nextUrl && currentScrollOffset >= scrollThreshold) {
393397
this.next();
394398

395399
// flag as initialized when rendering is completed
@@ -495,8 +499,14 @@
495499

496500
// ready is already fired, before on() could even be called, so
497501
// let's call the callback right away
498-
if (event === 'ready' && this.isInitialized) {
499-
$.proxy(callback, this)();
502+
if (this.isInitialized) {
503+
if (event === 'ready') {
504+
$.proxy(callback, this)();
505+
}
506+
// same applies to noneLeft
507+
else if (event === 'noneLeft' && !this.nextUrl) {
508+
$.proxy(callback, this)();
509+
}
500510
}
501511

502512
return this;
@@ -567,7 +577,6 @@
567577

568578
if (!self.nextUrl) {
569579
self.fire('noneLeft', [self.getLastItem()]);
570-
self.listeners['noneLeft'].disable(); // disable it so it only fires once
571580
}
572581

573582
self.resume();

test/02-listeners-test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ describe("IAS", function () {
171171
// register listener
172172
.on('noneLeft', spy1);
173173

174+
expect(spy1).not.toHaveBeenCalled();
175+
174176
// scroll to page 2
175177
scrollDown().then(function() {
176178
wait(2000).then(function() {
@@ -189,4 +191,62 @@ describe("IAS", function () {
189191

190192
return deferred.promise;
191193
});
194+
195+
it("should call noneLeft listeners when content is short without next (before init)", function() {
196+
var deferred = when.defer();
197+
var spy1 = this.spy();
198+
199+
jQuery.ias('destroy');
200+
201+
loadFixture("short-without-next.html", function() {
202+
var ias = jQuery.ias({
203+
container : '.listing',
204+
item: '.post',
205+
pagination: '.navigation',
206+
next: '.next-posts a',
207+
initialize: false
208+
});
209+
210+
// register listener
211+
ias.on('noneLeft', spy1);
212+
213+
expect(spy1).not.toHaveBeenCalled();
214+
215+
ias.initialize();
216+
217+
expect(spy1).toHaveBeenCalledOnce();
218+
219+
deferred.resolve();
220+
});
221+
222+
return deferred.promise;
223+
});
224+
225+
it("should call noneLeft listeners when content is short without next (after init)", function() {
226+
var deferred = when.defer();
227+
var spy1 = this.spy();
228+
229+
jQuery.ias('destroy');
230+
231+
loadFixture("short-without-next.html", function() {
232+
var ias = jQuery.ias({
233+
container : '.listing',
234+
item: '.post',
235+
pagination: '.navigation',
236+
next: '.next-posts a',
237+
initialize: false
238+
});
239+
240+
ias.initialize();
241+
242+
// register listener
243+
ias.on('noneLeft', spy1);
244+
245+
expect(spy1).toHaveBeenCalledOnce();
246+
247+
deferred.resolve();
248+
});
249+
250+
return deferred.promise;
251+
});
192252
});

test/buster.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ config["My tests"] = {
3232
{ path: "/page3.html", file: "test/fixtures/page3.html" },
3333
{ path: "/framed.html", file: "test/fixtures/framed.html" },
3434
{ path: "/short.html", file: "test/fixtures/short.html" },
35+
{ path: "/short-without-next.html", file: "test/fixtures/short-without-next.html" },
3536
{ path: "/ajax1.html", file: "test/fixtures/ajax1.html" },
3637
{ path: "/ajax2.html", file: "test/fixtures/ajax2.html" },
3738
{ path: "/multiple1.html", file: "test/fixtures/multiple1.html" },
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5+
<title>Very short Page 1</title>
6+
</head>
7+
<body>
8+
<div id="content">
9+
<div class="listing">
10+
<div class="post">
11+
<strong>item 1</strong>
12+
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis.</p>
13+
</div>
14+
15+
<div class="post">
16+
<strong>item 2</strong>
17+
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis.</p>
18+
</div>
19+
</div>
20+
21+
<div class="navigation">
22+
<ul>
23+
<li>1</li>
24+
</ul>
25+
</div>
26+
</div>
27+
28+
</body>
29+
</html>

0 commit comments

Comments
 (0)