Skip to content

Commit c8b16d1

Browse files
committed
🛠 add onInit option for binding initial events
fixes race condition on prefill tests
1 parent 805853b commit c8b16d1

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

‎.gitignore‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
infinte-scroll.tmproj
2-
.idea
3-
1+
bower_components/
2+
node_modules/

‎js/core.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ proto.create = function() {
6464
}
6565
this.updateGetAbsolutePath();
6666
this.log( 'initialized', [ this.element.className ] );
67+
this.callOnInit();
6768
// create features
6869
for ( var method in InfiniteScroll.create ) {
6970
InfiniteScroll.create[ method ].call( this );
@@ -74,6 +75,14 @@ proto.option = function( opts ) {
7475
utils.extend( this.options, opts );
7576
};
7677

78+
// call onInit option, used for binding events on init
79+
proto.callOnInit = function() {
80+
var onInit = this.options.onInit;
81+
if ( onInit ) {
82+
onInit.call( this, this );
83+
}
84+
};
85+
7786
// ----- events ----- //
7887

7988
proto.dispatchEvent = function( type, event, args ) {

‎test/.jshintrc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"unused": true,
55
"undef": true,
66
"globals": {
7-
"Episcroll": false,
7+
"InfiniteScroll": false,
88
"QUnit": false
99
}
10-
}
10+
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
QUnit.test( 'prefill element', function( assert ) {
22

33
// expected load count, each post is 200px tall
4-
var expLoadCount = 3
5-
6-
// assert.ok( true );
4+
var expLoadCount = 3;
75

86
var done = assert.async( expLoadCount );
97

@@ -16,15 +14,18 @@ QUnit.test( 'prefill element', function( assert ) {
1614
elementScroll: true,
1715
history: false,
1816
scrollThreshold: false,
17+
onInit: function() {
18+
this.on( 'append', onAppend );
19+
},
1920
});
2021

21-
infScroll.on( 'append', function() {
22+
function onAppend() {
2223
assert.ok( true, 'prefill element appended post ' + infScroll.loadCount );
2324
if ( infScroll.loadCount == expLoadCount ) {
2425
assert.equal( infScroll.loadCount, expLoadCount,
2526
expLoadCount + ' pages appended' );
2627
}
2728
done();
28-
});
29+
}
2930

3031
});

‎test/unit/prefill-window.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ QUnit.test( 'prefill window', function( assert ) {
22

33
// expected load count, each post is 200px tall
44
var expLoadCount = Math.ceil( window.innerHeight / 200 );
5-
65
var done = assert.async( expLoadCount );
76

87
var infScroll = new InfiniteScroll( '.demo--prefill-window', {
@@ -13,15 +12,18 @@ QUnit.test( 'prefill window', function( assert ) {
1312
prefill: true,
1413
history: false,
1514
scrollThreshold: false,
15+
onInit: function() {
16+
this.on( 'append', onAppend );
17+
},
1618
});
1719

18-
infScroll.on( 'append', function() {
20+
function onAppend() {
1921
assert.ok( true, 'prefill window appended post ' + infScroll.loadCount );
2022
if ( infScroll.loadCount == expLoadCount ) {
2123
assert.equal( infScroll.loadCount, expLoadCount,
2224
expLoadCount + ' pages appended' );
2325
}
2426
done();
25-
});
27+
}
2628

2729
});

0 commit comments

Comments
 (0)