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

Commit 3bf2953

Browse files
committed
Merge pull request #1488 from OpenBazaar/lazyLoadFollowers
Lazy load followers
2 parents 356c843 + c3f8c0b commit 3bf2953

File tree

4 files changed

+84
-34
lines changed

4 files changed

+84
-34
lines changed

js/languages/en-US.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"SearchForItemsPlaceholder": "Type #games, #shoes or any #tag...",
8787
"SearchForPagesPlaceholder": "Search by name or keyword",
8888
"SearchFeedPlaceholder": "Type a keyword...",
89-
"SearchForFollowersPlaceholder": "Type a name...",
89+
"SearchForFollowersPlaceholder": "Search for a name in your currently loaded followers (scroll to load more).",
9090
"SearchForUsersPlaceholder": "Type a name...",
9191
"SearchOnUserStorePlaceholder": "Type a title...",
9292
"EstDeliveryDomesticPlaceholder": "3-5 Business Days",
@@ -305,9 +305,9 @@
305305
"Off": "Off",
306306
"ClickToChange": "Click to change",
307307
"NotProvided": "not provided",
308-
"NotFollowingAnyone": "Not following anyone",
309-
"NoFollowers": "No followers",
310-
"NoReviews": "No reviews",
308+
"NotFollowingAnyone": "No nodes being followed were loaded",
309+
"NoFollowers": "No followers were loaded",
310+
"NoReviews": "No reviews were loaded",
311311
"NoDetailsSpecified": "No details specified",
312312
"Moderator": "Moderator",
313313
"Moderator2": "Moderator:",

js/templates/userPage.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,18 +250,18 @@ <h1 class="page-userNameLarge floatLeft fancy-heading fontSize20 colorWhiteForce
250250
<a class="btn btn-bar btn-tab custCol-secondary js-followingTab js-tab paddingRight18">
251251
<span class="ion-person fontSize11 marginRight2 textOpacity"></span>
252252
<%= polyglot.t('Following') %>
253-
<span class="pill fontSize12 textOpacity75 marginLeft2 js-userFollowingCount">0</span>
253+
<span class="pill fontSize12 textOpacity75 marginLeft2 js-userFollowingCount">...</span>
254254
</a>
255255
<a class="btn btn-bar btn-tab custCol-secondary js-followersTab js-tab paddingRight18">
256256
<span class="ion-person-stalker fontSize11 marginRight2 textOpacity1"></span>
257257
<%= polyglot.t('Followers') %>
258-
<span class="pill fontSize12 textOpacity75 marginLeft2 js-userFollowerCount">0</span>
258+
<span class="pill fontSize12 textOpacity75 marginLeft2 js-userFollowerCount">...</span>
259259
</a>
260260
<% if(ob.page.profile.vendor) { %>
261261
<a class="btn btn-bar btn-tab custCol-secondary js-storeTab js-tab paddingRight18">
262262
<span class="ion-ios-cart fontSize11 marginRight2 textOpacity1"></span>
263263
<%= polyglot.t('Store') %>
264-
<span class="pill fontSize12 textOpacity75 marginLeft2 js-listingCount">0</span>
264+
<span class="pill fontSize12 textOpacity75 marginLeft2 js-listingCount">...</span>
265265
<% } %>
266266
</a>
267267
</div>

js/views/userListVw.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ module.exports = Backbone.View.extend({
1717
options.serverUrl: server url to pass into each user view
1818
options.ownFollowing: array of guids this user is following
1919
options.hideFollow: boolean, hide follow button
20-
options.reverse; should the list of users be reversed?
20+
options.reverse: should the list of users be reversed?
21+
options.perFetch: the number of users returned per fetch (optional, only applies to the get_followers api)
22+
options.followerCount: the total number of followers (optional, only applise to the get_followers api)
2123
*/
2224
//the model must be passed in by the constructor
2325
this.usersShort = new usersShortCollection(this.model);
2426
this.options.reverse && this.usersShort.models.reverse();
2527
this.subViews = [];
2628
this.showPerScroll = 10;
29+
this.fetchMoreAfter = options.perFetch;
2730
this.nextUserToShow = 0;
28-
this.totalUsers = this.usersShort.length;
31+
this.fetchedUsers = this.usersShort.length;
32+
this.totalUsers = this.options.followerCount;
2933
this.$container = $('#obContainer');
3034

3135
//listen to scrolling on container
@@ -34,6 +38,11 @@ module.exports = Backbone.View.extend({
3438
);
3539
this.$container.on('scroll', this.scrollHandler);
3640

41+
this.listenTo(this.usersShort, 'add', (data)=>{
42+
this.fetchedUsers = this.usersShort.length;
43+
this.renderUserSet(this.nextUserToShow, this.nextUserToShow + this.showPerScroll);
44+
});
45+
3746
this.render();
3847
},
3948

@@ -70,7 +79,12 @@ module.exports = Backbone.View.extend({
7079
this.trigger('usersAdded');
7180
}
7281

73-
this.nextUserToShow = this.nextUserToShow >= this.totalUsers ? this.nextUserToShow : this.nextUserToShow + this.showPerScroll;
82+
this.nextUserToShow = this.nextUserToShow >= this.fetchedUsers ? this.nextUserToShow : this.nextUserToShow + this.showPerScroll;
83+
84+
if(this.fetchMoreAfter && end == this.fetchMoreAfter && this.fetchedUsers < this.totalUsers){
85+
this.fetchMoreAfter = this.fetchMoreAfter + this.options.perFetch;
86+
this.trigger('fetchMoreUsers');
87+
}
7488
},
7589

7690
renderUser: function(item){
@@ -88,6 +102,11 @@ module.exports = Backbone.View.extend({
88102
}
89103
},
90104

105+
addUsers: function(model) {
106+
//add more users to the collection
107+
this.usersShort.add(model);
108+
},
109+
91110
renderNoneFound: function(){
92111
var simpleMessage = new simpleMessageView({title: this.options.title, message: this.options.message, el: this.$el});
93112
this.subViews.push(simpleMessage);

js/views/userPageVw.js

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ module.exports = baseVw.extend({
183183
//set view's userID from the userModel;
184184
this.userID = options.userModel.get('guid');
185185
this.userProfileFetchParameters = {};
186+
this.followerFetchStart = 0;
187+
this.followersFetchPer = 30;
188+
this.followerFetchTotal = 0;
186189
this.itemFetchParameters = {};
187190
this.model = new Backbone.Model();
188191
this.globalUserProfile = options.userProfile;
@@ -663,6 +666,10 @@ module.exports = baseVw.extend({
663666
if(self.options.ownPage === false && Boolean(__.findWhere(followingArray, {guid: self.userID}))){
664667
self.$('.js-followsMe').removeClass('hide')
665668
}
669+
//mark whether page is being followed
670+
if(self.options.ownPage === false){
671+
self.toggleFollowButtons(Boolean(__.findWhere(followingArray, {guid: self.pageID})));
672+
}
666673

667674
}).fail(function(jqXHR, status, errorThrown){
668675
if (self.isRemoved()) return;
@@ -685,21 +692,34 @@ module.exports = baseVw.extend({
685692
});
686693
},
687694

688-
fetchFollowers: function(){
689-
var self = this;
695+
fetchFollowers: function(ignoreTotal){
696+
var self = this,
697+
fetchFollowersParameters;
690698

699+
if(!ignoreTotal && this.followerFetchStart > 0 && this.followerFetchStart >= this.followerFetchTotal){
700+
//don't fetch again if all of the followers have been fetched
701+
return;
702+
}
703+
704+
if(this.options.ownPage){
705+
fetchFollowersParameters = $.param({'start': this.followerFetchStart});
706+
} else {
707+
fetchFollowersParameters = $.param({'guid': this.pageID, 'start': this.followerFetchStart});
708+
}
709+
691710
this.followers.fetch({
692-
data: self.userProfileFetchParameters,
693-
//timeout: 5000,
694-
success: function(model){
695-
var followerArray = model.get('followers');
711+
data: fetchFollowersParameters,
712+
success: (model)=> {
713+
var followerArray = model.get('followers') || [];
714+
715+
this.followerFetchTotal = model.get('count') || followerArray.length; //the length is for older servers
716+
this.$('.js-userFollowerCount').html(this.followerFetchTotal);
696717

697718
if (self.isRemoved()) return;
698719

699-
self.renderFollowers(followerArray);
700-
//if this is not their page, see if they are being followed
701-
if(self.options.ownPage === false){
702-
self.toggleFollowButtons(Boolean(__.findWhere(followerArray, {guid: self.userID})));
720+
if(followerArray.length || this.followerFetchTotal == 0){
721+
//always render the first time so the no followers message is shown for no followers
722+
this.renderFollowers(followerArray, this.followerFetchTotal);
703723
}
704724
},
705725
error: function(model, response){
@@ -713,6 +733,7 @@ module.exports = baseVw.extend({
713733
}
714734
}
715735
});
736+
this.followerFetchStart += this.followersFetchPer;
716737
},
717738

718739
getIsModerator: function () {
@@ -783,25 +804,31 @@ module.exports = baseVw.extend({
783804
}
784805
},
785806

786-
renderFollowers: function (model) {
807+
renderFollowers: function (model, followerCount) {
787808
"use strict";
788809

789810
model = model || [];
790-
this.followerList = new personListView({
791-
model: model,
792-
el: '.js-list1',
793-
title: window.polyglot.t('NoFollowers'),
794-
message: "",
795-
ownFollowing: this.ownFollowing,
796-
hideFollow: true,
797-
serverUrl: this.options.userModel.get('serverUrl'),
798-
reverse: true
799-
});
800-
this.registerChild(this.followerList);
801-
802-
this.$('.js-userFollowerCount').html(model.length);
811+
//if view doesn't exist, create it
812+
if(!this.followerList) {
813+
this.followerList = new personListView({
814+
model: model,
815+
el: '.js-list1',
816+
title: window.polyglot.t('NoFollowers'),
817+
message: "",
818+
ownFollowing: this.ownFollowing,
819+
hideFollow: true,
820+
serverUrl: this.options.userModel.get('serverUrl'),
821+
reverse: true,
822+
perFetch: 30,
823+
followerCount: followerCount
824+
});
825+
this.registerChild(this.followerList);
826+
}else if(model.length) {
827+
this.followerList.addUsers(model);
828+
}
803829

804830
if (model.length) {
831+
//refresh search
805832
this.followersSearch = new window.List('searchFollowers', {
806833
valueNames: ['js-searchName', 'js-searchHandle'],
807834
page: 1000
@@ -813,6 +840,10 @@ module.exports = baseVw.extend({
813840
this.followersSearch.reIndex();
814841
searchTerms && this.followersSearch.search(searchTerms);
815842
});
843+
844+
this.listenTo(this.followerList, 'fetchMoreUsers', ()=>{
845+
this.fetchFollowers();
846+
});
816847
},
817848

818849
renderFollowing: function (model) {

0 commit comments

Comments
 (0)