Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit b78baa7

Browse files
committed
fix overfetching issue
1 parent bae51d1 commit b78baa7

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

js/views/userListVw.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,20 @@ module.exports = Backbone.View.extend({
3131
this.fetchedUsers = this.usersShort.length;
3232
this.totalUsers = this.options.followerCount;
3333
this.$container = $('#obContainer');
34+
this.fetching = false;
3435

3536
//listen to scrolling on container
3637
this.scrollHandler = __.bind(
3738
__.throttle(this.onScroll, 100), this
3839
);
3940
this.$container.on('scroll', this.scrollHandler);
4041

42+
this.listenTo(this.usersShort, 'add', (data)=>{
43+
this.fetchedUsers = this.usersShort.length;
44+
this.fetching = false;
45+
this.renderUserSet(this.nextUserToShow, this.nextUserToShow + this.showPerScroll);
46+
});
47+
4148
this.render();
4249
},
4350

@@ -60,8 +67,6 @@ module.exports = Backbone.View.extend({
6067
return (index >= start) && (index < end);
6168
});
6269

63-
this.fetchedUsers = this.usersShort.length;
64-
6570
__.each(renderSet, function(user) {
6671
user.set('avatarURL', self.options.serverUrl+"get_image?hash="+user.get('avatar_hash')+"&guid="+user.get('guid'));
6772
if(self.options.ownFollowing.indexOf(user.get('guid')) != -1){
@@ -77,14 +82,11 @@ module.exports = Backbone.View.extend({
7782
}
7883

7984
this.nextUserToShow = this.nextUserToShow >= this.fetchedUsers ? this.nextUserToShow : this.nextUserToShow + this.showPerScroll;
80-
console.log("f: " + this.fetchedUsers)
81-
console.log("t: " + this.totalUsers)
82-
console.log("e: " + end)
83-
console.log("fm: " + this.fetchMoreAfter)
84-
if(this.fetchMoreAfter && end == this.fetchMoreAfter && this.fetchedUsers != this.totalUsers){
85+
86+
if(this.fetchMoreAfter && end == this.fetchMoreAfter && this.fetchedUsers < this.totalUsers && !this.fetching){
8587
this.fetchMoreAfter = this.fetchMoreAfter + this.options.perFetch;
8688
this.trigger('fetchMoreUsers');
87-
console.log("fetch more from userlistvw")
89+
this.fetching = true;
8890
}
8991
},
9092

@@ -105,9 +107,7 @@ module.exports = Backbone.View.extend({
105107

106108
addUsers: function(model) {
107109
//add more users to the collection
108-
console.log("add users")
109110
this.usersShort.add(model);
110-
this.renderUserSet(this.nextUserToShow, this.nextUserToShow + this.showPerScroll);
111111
},
112112

113113
renderNoneFound: function(){

js/views/userPageVw.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ module.exports = baseVw.extend({
184184
this.userID = options.userModel.get('guid');
185185
this.userProfileFetchParameters = {};
186186
this.followerFetchStart = 0;
187+
this.followersFetchPer = 30;
188+
this.followerFetchTotal = 0;
187189
this.itemFetchParameters = {};
188190
this.model = new Backbone.Model();
189191
this.globalUserProfile = options.userProfile;
@@ -690,30 +692,33 @@ module.exports = baseVw.extend({
690692
});
691693
},
692694

693-
fetchFollowers: function(){
695+
fetchFollowers: function(ignoreTotal){
694696
var self = this,
695697
fetchFollowersParameters;
696698

699+
if(!ignoreTotal && this.followerFetchStart > 0 && this.followerFetchStart >= this.followerFetchTotal){
700+
return;
701+
}
702+
697703
if(this.options.ownPage){
698704
fetchFollowersParameters = $.param({'start': this.followerFetchStart});
699705
} else {
700706
fetchFollowersParameters = $.param({'guid': this.pageID, 'start': this.followerFetchStart});
701707
}
702-
703-
console.log("fetchFollowers")
704-
708+
705709
this.followers.fetch({
706710
data: fetchFollowersParameters,
707711
//timeout: 5000,
708712
success: (model)=> {
709-
var followerArray = model.get('followers'),
710-
followerCount = model.get('count') || followerArray.length;
711-
this.$('.js-userFollowerCount').html(followerCount);
712-
this.followerFetchStart += followerArray.length;
713+
var followerArray = model.get('followers');
714+
this.followerFetchTotal = model.get('count') || followerArray.length;
715+
this.$('.js-userFollowerCount').html(this.followerFetchTotal);
713716

714717
if (self.isRemoved()) return;
715-
716-
this.renderFollowers(followerArray, followerCount);
718+
719+
if(followerArray.length){
720+
this.renderFollowers(followerArray, this.followerFetchTotal);
721+
}
717722
},
718723
error: function(model, response){
719724
if (self.isRemoved()) return;
@@ -726,6 +731,7 @@ module.exports = baseVw.extend({
726731
}
727732
}
728733
});
734+
this.followerFetchStart += this.followersFetchPer;
729735
},
730736

731737
getIsModerator: function () {
@@ -814,7 +820,7 @@ module.exports = baseVw.extend({
814820
followerCount: followerCount
815821
});
816822
this.registerChild(this.followerList);
817-
}else{
823+
}else if(model.length) {
818824
this.followerList.addUsers(model);
819825
}
820826

@@ -832,7 +838,6 @@ module.exports = baseVw.extend({
832838
});
833839

834840
this.listenTo(this.followerList, 'fetchMoreUsers', ()=>{
835-
console.log("trigger fetch");
836841
this.fetchFollowers();
837842
});
838843
},

0 commit comments

Comments
 (0)