Skip to content

Commit be2bb67

Browse files
committed
Merge pull request #171 from fieg/ajax
Ajax improvements and more
2 parents 62f5c2b + df88fb4 commit be2bb67

27 files changed

+398
-128
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Infinite Ajax Scroll
1+
Infinite AJAX Scroll
22
====================
33

44
A jQuery plugin to turn your paginated pages into infinite scrolling pages with ease.

doc/methods.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jQuery.ias().bind();
1111

1212
### destroy
1313

14-
Alias of [unbind](methods.html#unbind) method.
14+
Unbinds and destroys instance.
1515

1616
### extension
1717

@@ -29,6 +29,14 @@ Initializes Infinite AJAX Scroll. Normally this happens when the DOM is ready (`
2929
jQuery.ias().initialize();
3030
```
3131

32+
### reinitialize
33+
34+
Reinitializes Infinite AJAX Scroll after a DOM update. DOM updates could be made by page updates via AJAX, like changing the sorting of a list, or filtering a result.
35+
36+
```javascript
37+
jQuery.ias().reinitialize();
38+
```
39+
3240
### next
3341

3442
Loads the next page.

src/callbacks.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ var IASCallbacks = function () {
2323
var context = args[0],
2424
deferred = args[1],
2525
callbackArguments = args[2];
26+
2627
this.isFiring = true;
2728

2829
for (var i = 0, l = this.list.length; i < l; i++) {
29-
if (false === this.list[i].fn.apply(context, callbackArguments)) {
30-
deferred.reject();
30+
if (this.list[i] != undefined) {
31+
if (false === this.list[i].fn.apply(context, callbackArguments)) {
32+
deferred.reject();
3133

32-
break;
34+
break;
35+
}
3336
}
3437
}
3538

src/extension/history.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ var IASHistoryExtension = function (options) {
2525
* @param url
2626
*/
2727
this.onPageChange = function (pageNum, scrollOffset, url) {
28-
var state = {};
29-
3028
if (!window.history || !window.history.replaceState) {
3129
return;
3230
}
3331

34-
state = history.state;
32+
var state = history.state;
3533

3634
history.replaceState(state, document.title, url);
3735
};
@@ -55,6 +53,17 @@ var IASHistoryExtension = function (options) {
5553
}
5654
};
5755

56+
this.onReady = function () {
57+
var currentScrollOffset = this.ias.getCurrentScrollOffset(this.ias.$scrollContainer),
58+
firstItemScrollThreshold = this.getScrollThresholdFirstItem();
59+
60+
currentScrollOffset -= this.ias.$scrollContainer.height();
61+
62+
if (currentScrollOffset <= firstItemScrollThreshold) {
63+
this.prev();
64+
}
65+
};
66+
5867
/**
5968
* Returns the url for the next page
6069
*
@@ -83,7 +92,7 @@ var IASHistoryExtension = function (options) {
8392

8493
// if the don't have a first element, the DOM might not have been loaded,
8594
// or the selector is invalid
86-
if (0 === $firstElement.size()) {
95+
if (0 === $firstElement.length) {
8796
return -1;
8897
}
8998

@@ -150,20 +159,19 @@ IASHistoryExtension.prototype.initialize = function (ias) {
150159
* @param ias
151160
*/
152161
IASHistoryExtension.prototype.bind = function (ias) {
153-
var self = this;
154-
155162
ias.on('pageChange', jQuery.proxy(this.onPageChange, this));
156163
ias.on('scroll', jQuery.proxy(this.onScroll, this));
157-
ias.on('ready', function () {
158-
var currentScrollOffset = ias.getCurrentScrollOffset(ias.$scrollContainer),
159-
firstItemScrollThreshold = self.getScrollThresholdFirstItem();
160-
161-
currentScrollOffset -= ias.$scrollContainer.height();
164+
ias.on('ready', jQuery.proxy(this.onReady, this));
165+
};
162166

163-
if (currentScrollOffset <= firstItemScrollThreshold) {
164-
self.prev();
165-
}
166-
});
167+
/**
168+
* @public
169+
* @param {object} ias
170+
*/
171+
IASHistoryExtension.prototype.unbind = function(ias) {
172+
ias.off('pageChange', this.onPageChange);
173+
ias.off('scroll', this.onScroll);
174+
ias.off('ready', this.onReady);
167175
};
168176

169177
/**
@@ -180,7 +188,7 @@ IASHistoryExtension.prototype.prev = function () {
180188
return false;
181189
}
182190

183-
ias.unbind();
191+
ias.pause();
184192

185193
var promise = ias.fire('prev', [url]);
186194

@@ -189,7 +197,7 @@ IASHistoryExtension.prototype.prev = function () {
189197
self.renderBefore(items, function () {
190198
self.prevUrl = self.getPrevUrl(data);
191199

192-
ias.bind();
200+
ias.resume();
193201

194202
if (self.prevUrl) {
195203
self.prev();
@@ -199,7 +207,7 @@ IASHistoryExtension.prototype.prev = function () {
199207
});
200208

201209
promise.fail(function () {
202-
ias.bind();
210+
ias.resume();
203211
});
204212

205213
return true;

src/extension/noneleft.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ IASNoneLeftExtension.prototype.bind = function(ias) {
3838
ias.on('noneLeft', jQuery.proxy(this.showNoneLeft, this));
3939
};
4040

41+
/**
42+
* @public
43+
* @param {object} ias
44+
*/
45+
IASNoneLeftExtension.prototype.unbind = function(ias) {
46+
ias.off('noneLeft', this.showNoneLeft);
47+
};
48+
4149
/**
4250
* @public
4351
*/

src/extension/paging.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ IASPagingExtension.prototype.bind = function(ias) {
118118
ias.on('scroll', jQuery.proxy(this.onScroll, this), this.priority);
119119
};
120120

121+
/**
122+
* @public
123+
* @param {object} ias
124+
*/
125+
IASPagingExtension.prototype.unbind = function(ias) {
126+
try {
127+
ias.off('prev', this.onPrev);
128+
} catch (exception) {}
129+
130+
ias.off('next', this.onNext);
131+
ias.off('scroll', this.onScroll);
132+
};
133+
121134
/**
122135
* Returns current page number based on scroll offset
123136
*

src/extension/spinner.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var IASSpinnerExtension = function(options) {
5353
this.getSpinner = function() {
5454
var $spinner = jQuery('#ias_spinner_' + this.uid);
5555

56-
if ($spinner.size() > 0) {
56+
if ($spinner.length > 0) {
5757
return $spinner;
5858
}
5959

@@ -66,7 +66,7 @@ var IASSpinnerExtension = function(options) {
6666
this.hasSpinner = function() {
6767
var $spinner = jQuery('#ias_spinner_' + this.uid);
6868

69-
return ($spinner.size() > 0);
69+
return ($spinner.length > 0);
7070
};
7171

7272
/**
@@ -90,12 +90,24 @@ IASSpinnerExtension.prototype.bind = function(ias) {
9090
this.ias = ias;
9191

9292
ias.on('next', jQuery.proxy(this.showSpinner, this));
93+
ias.on('render', jQuery.proxy(this.removeSpinner, this));
9394

9495
try {
9596
ias.on('prev', jQuery.proxy(this.showSpinnerBefore, this));
9697
} catch (exception) {}
98+
};
9799

98-
ias.on('render', jQuery.proxy(this.removeSpinner, this));
100+
/**
101+
* @public
102+
* @param {object} ias
103+
*/
104+
IASSpinnerExtension.prototype.unbind = function(ias) {
105+
ias.off('next', this.showSpinner);
106+
ias.off('render', this.removeSpinner);
107+
108+
try {
109+
ias.off('prev', this.showSpinnerBefore);
110+
} catch (exception) {}
99111
};
100112

101113
/**

src/extension/trigger.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ var IASTriggerExtension = function(options) {
5858
return false;
5959
};
6060

61+
this.onRendered = function() {
62+
this.enabled = true;
63+
};
64+
6165
/**
6266
* @param clickCallback
6367
* @returns {*|jQuery}
@@ -88,20 +92,33 @@ IASTriggerExtension.prototype.bind = function(ias) {
8892

8993
this.ias = ias;
9094

95+
ias.on('next', jQuery.proxy(this.showTriggerNext, this), this.priority);
96+
ias.on('rendered', jQuery.proxy(this.onRendered, this), this.priority);
97+
9198
try {
9299
ias.on('prev', jQuery.proxy(this.showTriggerPrev, this), this.priority);
93100
} catch (exception) {}
101+
};
94102

95-
ias.on('next', jQuery.proxy(this.showTriggerNext, this), this.priority);
96-
ias.on('rendered', function () { self.enabled = true; }, this.priority);
103+
/**
104+
* @public
105+
* @param {object} ias
106+
*/
107+
IASTriggerExtension.prototype.unbind = function(ias) {
108+
ias.off('next', this.showTriggerNext);
109+
ias.off('rendered', this.onRendered);
110+
111+
try {
112+
ias.off('prev', this.showTriggerPrev);
113+
} catch (exception) {}
97114
};
98115

99116
/**
100117
* @public
101118
*/
102119
IASTriggerExtension.prototype.next = function() {
103120
this.enabled = false;
104-
this.ias.unbind();
121+
this.ias.pause();
105122

106123
if (this.$triggerNext) {
107124
this.$triggerNext.remove();
@@ -116,7 +133,7 @@ IASTriggerExtension.prototype.next = function() {
116133
*/
117134
IASTriggerExtension.prototype.prev = function() {
118135
this.enabled = false;
119-
this.ias.unbind();
136+
this.ias.pause();
120137

121138
if (this.$triggerPrev) {
122139
this.$triggerPrev.remove();

0 commit comments

Comments
 (0)