Skip to content

Commit 7512e46

Browse files
authored
Merge pull request #414 from fieg/scope-fixes
Fix various scope issues
2 parents 532076b + d0edfe9 commit 7512e46

File tree

5 files changed

+32
-35
lines changed

5 files changed

+32
-35
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
<h1 align="center">Infinite Ajax Scroll</h1>
88

9-
**Note: You're viewing the 3.x branch which is in development. See the [2.x](https://github.com/webcreate/infinite-ajax-scroll/tree/2.x) branch for the latest stable version.**
9+
**Note: You're viewing the 3.x branch which is in development. See the [2.x](https://github.com/webcreate/infinite-ajax-scroll/tree/2.x) branch for the latest stable version.**
1010

11-
Turn your existing pagination into infinite scrolling pages with ease.
11+
Turn your existing pagination into infinite scrolling pages with ease.
1212

1313
* SEO friendly 🥇
1414
* Doesn't break browsers back button 💯
1515
* Highly customizable ✨
1616

17-
More features, documentation and examples available at: https://docs.v3.infiniteajaxscroll.com/
17+
More features, documentation and examples available at: https://docs.infiniteajaxscroll.com/
1818

1919
[![Build Status](https://travis-ci.org/webcreate/infinite-ajax-scroll.svg?branch=3.x)](https://travis-ci.org/webcreate/infinite-ajax-scroll)
2020

@@ -68,7 +68,7 @@ let ias = new InfiniteAjaxScroll('.container', {
6868
});
6969
```
7070

71-
Full documentation can be found at https://docs.v3.infiniteajaxscroll.com
71+
Full documentation can be found at https://docs.infiniteajaxscroll.com
7272

7373
## Licensing
7474

src/event-handlers.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
import {getScrollPosition} from './dimensions';
22

3-
// @todo 1) verify that this is NOT shared among multiple instances
4-
// @todo 2) fill in these values on bind instead of zeroing them all
5-
let lastScroll = {
3+
const defaultLastScroll = {
64
y: 0,
75
x: 0,
86
deltaY: 0,
97
deltaX: 0
108
};
119

12-
function calculateScroll(scrollContainer) {
10+
function calculateScroll(scrollContainer, lastScroll) {
1311
let scroll = getScrollPosition(scrollContainer);
1412

1513
scroll.deltaY = scroll.y - (lastScroll ? lastScroll.y : scroll.y);
1614
scroll.deltaX = scroll.x - (lastScroll ? lastScroll.x : scroll.x);
1715

18-
lastScroll = scroll;
19-
2016
return scroll;
2117
}
2218

2319
export function scrollHandler() {
24-
const scroll = calculateScroll(this.scrollContainer);
20+
let ias = this;
21+
let lastScroll = ias._lastScroll || defaultLastScroll;
22+
23+
const scroll = ias._lastScroll = calculateScroll(ias.scrollContainer, lastScroll);
2524

2625
this.emitter.emit('scrolled', {scroll});
2726

2827
this.measure();
2928
}
3029

3130
export function resizeHandler() {
32-
const scroll = calculateScroll(this.scrollContainer);
31+
let ias = this;
32+
let lastScroll = ias._lastScroll || defaultLastScroll;
33+
34+
const scroll = ias._lastScroll = calculateScroll(ias.scrollContainer, lastScroll);
3335

3436
this.emitter.emit('resized', {scroll});
3537

src/infinite-ajax-scroll.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import Logger from './logger';
1313
import Paging from './paging';
1414
import {appendFn} from './append';
1515

16-
let scrollListener;
17-
let resizeListener;
18-
1916
export default class InfiniteAjaxScroll {
2017
constructor(container, options = {}) {
2118
Assert.singleElement(container, 'container');
@@ -70,11 +67,11 @@ export default class InfiniteAjaxScroll {
7067
return;
7168
}
7269

73-
scrollListener = throttle(scrollHandler, 200).bind(this);
74-
resizeListener = throttle(resizeHandler, 200).bind(this);
70+
this._scrollListener = throttle(scrollHandler, 200).bind(this);
71+
this._resizeListener = throttle(resizeHandler, 200).bind(this);
7572

76-
this.scrollContainer.addEventListener('scroll', scrollListener);
77-
this.scrollContainer.addEventListener('resize', resizeListener);
73+
this.scrollContainer.addEventListener('scroll', this._scrollListener);
74+
this.scrollContainer.addEventListener('resize', this._resizeListener);
7875

7976
this.binded = true;
8077

@@ -86,8 +83,8 @@ export default class InfiniteAjaxScroll {
8683
return;
8784
}
8885

89-
this.scrollContainer.removeEventListener('resize', resizeListener);
90-
this.scrollContainer.removeEventListener('scroll', scrollListener);
86+
this.scrollContainer.removeEventListener('resize', this._resizeListener);
87+
this.scrollContainer.removeEventListener('scroll', this._scrollListener);
9188

9289
this.binded = false;
9390

src/logger.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ const defaultLogger = {
1010
unbinded: () => {
1111
console.log(`Unbinded event handlers`);
1212
},
13-
// scrolled: () => {
13+
// scrolled: (event) => {
1414
// console.log('Scrolled');
1515
// },
16-
// resized: () => {
16+
// resized: (event) => {
1717
// console.log('Resized');
1818
// },
1919
next: (event) => {

src/next-handler.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
import $ from 'tealight';
22

3-
let lastResponse = document;
4-
let nextUrl;
5-
63
export function nextHandler(pageIndex) {
74
let ias = this;
5+
let lastResponse = ias._lastResponse || document;
86

97
let nextEl = $(ias.options.next, lastResponse)[0];
108

119
if (!nextEl) {
1210
return;
1311
}
1412

15-
nextUrl = nextEl.href;
13+
let nextUrl = nextEl.href;
1614

1715
return ias.load(nextUrl)
18-
.then((data) => {
19-
lastResponse = data.xhr.response;
16+
.then((data) => {
17+
lastResponse = ias._lastResponse = data.xhr.response;
2018

21-
let nextEl = $(ias.options.next, lastResponse)[0];
19+
let nextEl = $(ias.options.next, lastResponse)[0];
2220

23-
return ias.append(data.items)
24-
.then(() => {
25-
return !!nextEl;
26-
});
27-
});
21+
return ias.append(data.items)
22+
.then(() => {
23+
return !!nextEl;
24+
});
25+
});
2826
}

0 commit comments

Comments
 (0)