Skip to content

Commit 5ddbfb3

Browse files
authored
Merge pull request #23 from dragantl/master
Added update to handle low number of initial items
2 parents 72e00e3 + a83855d commit 5ddbfb3

File tree

3 files changed

+75
-18
lines changed

3 files changed

+75
-18
lines changed

example/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
}
5151

5252
this.list = this.list.concat(temp);
53-
this.$broadcast('$InfiniteLoading:loaded');
53+
this.$nextTick(function () {
54+
this.$broadcast('$InfiniteLoading:loaded');
55+
});
5456
}.bind(this), 1000);
5557
}
5658
}

src/components/InfiniteLoading.vue

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,8 @@
8484
this.scrollParent = getScrollParent(this.$el);
8585
8686
this.scrollHandler = function scrollHandlerOriginal() {
87-
const currentDistance = getCurrentDistance(this.scrollParent);
88-
if (!this.isLoading && currentDistance <= this.distance) {
89-
this.isLoading = true;
90-
this.onInfinite.call();
87+
if (!this.isLoading) {
88+
this.attemptLoad();
9189
}
9290
}.bind(this);
9391
@@ -96,8 +94,10 @@
9694
},
9795
events: {
9896
'$InfiniteLoading:loaded': function loaded() {
99-
this.isLoading = false;
10097
this.isFirstLoad = false;
98+
if (this.isLoading) {
99+
this.attemptLoad();
100+
}
101101
},
102102
'$InfiniteLoading:complete': function complete() {
103103
this.isLoading = false;
@@ -112,6 +112,17 @@
112112
setTimeout(this.scrollHandler, 1);
113113
},
114114
},
115+
methods: {
116+
attemptLoad() {
117+
const currentDistance = getCurrentDistance(this.scrollParent);
118+
if (!this.isComplete && currentDistance <= this.distance) {
119+
this.isLoading = true;
120+
this.onInfinite.call();
121+
} else {
122+
this.isLoading = false;
123+
}
124+
},
125+
},
115126
destroyed() {
116127
if (!this.isComplete) {
117128
this.scrollParent.removeEventListener('scroll', this.scrollHandler);

test/unit/specs/InfiniteLoading.spec.js

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,27 @@ describe('InfiniteLoading.vue', () => {
1919
isLoadedAll: false,
2020
isDivScroll: true,
2121
isCustomSpinner: false,
22+
listContainerHeight: 100,
23+
listItemHeight: 10,
24+
customSpinnerHeight: 10
2225
},
2326
template: `
24-
<div style="height: 100px;"
27+
<div style="margin: 0; padding: 0;"
2528
:style="{
26-
overflow: isDivScroll ? 'auto' : 'visible'
29+
overflow: isDivScroll ? 'auto' : 'visible',
30+
height: listContainerHeight + 'px'
2731
}">
28-
<ul>
29-
<li v-for="item in list" v-text="item"></li>
32+
<ul style="margin: 0; padding: 0; font-size: 5px;">
33+
<li v-for="item in list" v-text="item" style="height: 10px; margin: 0; padding: 0;" :style="{
34+
height: listItemHeight + 'px'
35+
}"></li>
3036
</ul>
3137
<infinite-loading :distance="distance"
3238
:on-infinite="onInfinite"
3339
v-if="!isLoadedAll">
34-
<span slot="spinner" v-if="isCustomSpinner"><i class="custom-spinner"></i></span>
40+
<span slot="spinner" v-if="isCustomSpinner"><i class="custom-spinner" style="display: inline-block; width: 10px;" :style="{
41+
height: customSpinnerHeight + 'px'
42+
}"></i></span>
3543
</infinite-loading>
3644
</div>
3745
`,
@@ -103,23 +111,59 @@ describe('InfiniteLoading.vue', () => {
103111

104112
it('should display no results prompt', (done) => {
105113
vm.onInfinite = function test() {
106-
this.$broadcast('$InfiniteLoading:complete');
107114
Vue.nextTick(() => {
108-
expect(isShow(vm.$el.querySelectorAll('.infinite-status-prompt')[0])).to.be.true;
109-
done();
115+
this.$broadcast('$InfiniteLoading:complete');
116+
Vue.nextTick(() => {
117+
expect(isShow(vm.$el.querySelectorAll('.infinite-status-prompt')[0])).to.be.true;
118+
done();
119+
});
110120
});
111121
}.bind(vm);
112122

113123
vm.$mount().$appendTo('body');
114124
});
115125

116126
it('should display no more data prompt', (done) => {
127+
let i = 0;
117128
vm.onInfinite = function test() {
118-
this.$broadcast('$InfiniteLoading:loaded');
119-
this.$broadcast('$InfiniteLoading:complete');
129+
this.list.push(++i);
120130
Vue.nextTick(() => {
121-
expect(isShow(vm.$el.querySelectorAll('.infinite-status-prompt')[1])).to.be.true;
122-
done();
131+
this.$broadcast('$InfiniteLoading:loaded');
132+
this.$broadcast('$InfiniteLoading:complete');
133+
Vue.nextTick(() => {
134+
expect(isShow(vm.$el.querySelectorAll('.infinite-status-prompt')[1])).to.be.true;
135+
done();
136+
});
137+
});
138+
}.bind(vm);
139+
140+
vm.$mount().$appendTo('body');
141+
});
142+
143+
it('should load results to fill up the container', function (done) {
144+
const TEST_TIMEOUT = 2000;
145+
let mocha = this;
146+
let i = 0;
147+
vm.listContainerHeight = 100;
148+
vm.listItemHeight = 10;
149+
vm.distance = 10;
150+
vm.isCustomSpinner = true;
151+
vm.customSpinnerHeight = 10;
152+
let expectedCount = Math.floor(vm.listContainerHeight / vm.listItemHeight);
153+
vm.onInfinite = function test() {
154+
this.list.push(++i);
155+
Vue.nextTick(() => {
156+
this.$broadcast('$InfiniteLoading:loaded');
157+
if (i == expectedCount) {
158+
mocha.timeout(TEST_TIMEOUT + 100);
159+
setTimeout(function () {
160+
if (i == expectedCount) {
161+
done();
162+
} else {
163+
done(new Error('Unexpected number of items added'));
164+
}
165+
}, TEST_TIMEOUT);
166+
}
123167
});
124168
}.bind(vm);
125169

0 commit comments

Comments
 (0)