Skip to content

Commit 8308e10

Browse files
authored
Merge pull request #305 from fieg/auto-initialize
Added initialize option
2 parents 93e36e8 + f6b44a7 commit 8308e10

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

doc/options.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,17 @@ For example:
8383
Setting a `negativeMargin` of 250 means that IAS will start loading 250 pixel before the last item has scrolled into view.
8484

8585
Note: user experience can degrade if new pages are loaded too quickly without visual feedback (also see [delay](options.html#delay)). Use with caution.
86+
87+
### initialize
88+
89+
<dl>
90+
<dt>Type</dt>
91+
<dd>boolean</dd>
92+
93+
<dt>Default</dt>
94+
<dd>true</dd>
95+
</dl>
96+
97+
By default IAS initializes when the document is ready. During initialisation IAS binds to the scroll event and prefills the container when the content is shorter then the page fold.
98+
99+
If you want to control when IAS initializes yourself, you can set this value to false.

src/jquery-ias.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,12 @@
487487

488488
this.listeners[event].add($.proxy(callback, this), priority);
489489

490+
// ready is already fired, before on() could even be called, so
491+
// let's call the callback right away
492+
if (event === 'ready' && this.isInitialized) {
493+
$.proxy(callback, this)();
494+
}
495+
490496
return this;
491497
};
492498

@@ -627,7 +633,9 @@
627633
if (!instance) {
628634
$this.data('ias', (instance = new IAS($this, options)));
629635

630-
$(document).ready($.proxy(instance.initialize, instance));
636+
if (options.initialize) {
637+
$(document).ready($.proxy(instance.initialize, instance));
638+
}
631639
}
632640

633641
// when the plugin is called with a method
@@ -658,6 +666,7 @@
658666
next: '.next',
659667
pagination: false,
660668
delay: 600,
661-
negativeMargin: 10
669+
negativeMargin: 10,
670+
initialize: true
662671
};
663672
})(jQuery);

test/08-auto-initialize-test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
describe("auto initialize", function () {
2+
before(function() {
3+
this.timeout = 10000;
4+
5+
window.scrollTo(0, 0);
6+
});
7+
8+
after(function() {
9+
jQuery.ias('destroy');
10+
});
11+
12+
it("should initialize", function() {
13+
var deferred = when.defer();
14+
var spy = this.spy();
15+
16+
var ias = jQuery.ias({
17+
container : '.listing',
18+
item: '.post',
19+
pagination: '.navigation',
20+
next: '.next-posts a'
21+
});
22+
23+
ias.on('ready', spy);
24+
25+
wait(300).then(function() {
26+
expect(spy).toHaveBeenCalledOnce();
27+
28+
deferred.resolve();
29+
});
30+
31+
return deferred.promise;
32+
});
33+
34+
it("should not initialize", function() {
35+
var deferred = when.defer();
36+
var spy = this.spy();
37+
38+
var ias = jQuery.ias({
39+
container : '.listing',
40+
item: '.post',
41+
pagination: '.navigation',
42+
next: '.next-posts a',
43+
initialize: false
44+
});
45+
46+
ias.on('ready', spy);
47+
48+
wait(300).then(function() {
49+
expect(spy).not.toHaveBeenCalled();
50+
51+
deferred.resolve();
52+
});
53+
54+
return deferred.promise;
55+
});
56+
});

0 commit comments

Comments
 (0)