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

Commit b5096a9

Browse files
committed
Wire in fetching for lazy loading
1 parent 1d4f4a9 commit b5096a9

File tree

3 files changed

+65
-28
lines changed

3 files changed

+65
-28
lines changed

js/collections/usersShortCl.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ module.exports = Backbone.Collection.extend({
66
model: userShort,
77

88
initialize: function(models, options){
9-
this.fetchURL = options.fetchURL;
9+
10+
/*
11+
if (!options.fetchURL) {
12+
throw new Error('Please provide a collection.');
13+
}
14+
15+
this.fetchURL = options.fetchURL;*/
1016
},
1117

18+
1219
url: function() {
1320
return app.serverConfigs.getActive().getServerBaseUrl() + '/' + this.fetchURL;
1421
}

js/views/userListVw.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = Backbone.View.extend({
1919
options.hideFollow: boolean, hide follow button
2020
options.reverse: should the list of users be reversed?
2121
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)
2223
*/
2324
//the model must be passed in by the constructor
2425
this.usersShort = new usersShortCollection(this.model);
@@ -27,7 +28,8 @@ module.exports = Backbone.View.extend({
2728
this.showPerScroll = 10;
2829
this.fetchMoreAfter = options.perFetch;
2930
this.nextUserToShow = 0;
30-
this.totalUsers = this.usersShort.length;
31+
this.fetchedUsers = this.usersShort.length;
32+
this.totalUsers = this.options.followerCount;
3133
this.$container = $('#obContainer');
3234

3335
//listen to scrolling on container
@@ -58,6 +60,8 @@ module.exports = Backbone.View.extend({
5860
return (index >= start) && (index < end);
5961
});
6062

63+
this.fetchedUsers = this.usersShort.length;
64+
6165
__.each(renderSet, function(user) {
6266
user.set('avatarURL', self.options.serverUrl+"get_image?hash="+user.get('avatar_hash')+"&guid="+user.get('guid'));
6367
if(self.options.ownFollowing.indexOf(user.get('guid')) != -1){
@@ -72,9 +76,15 @@ module.exports = Backbone.View.extend({
7276
this.trigger('usersAdded');
7377
}
7478

75-
this.nextUserToShow = this.nextUserToShow >= this.totalUsers ? this.nextUserToShow : this.nextUserToShow + this.showPerScroll;
76-
if(this.fetchMoreAfter && this.totalUsers == this.fetchMoreAfter){
77-
this.trigger('fetch')
79+
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+
this.fetchMoreAfter = this.fetchMoreAfter + this.options.perFetch;
86+
this.trigger('fetchMoreUsers');
87+
console.log("fetch more from userlistvw")
7888
}
7989
},
8090

@@ -93,6 +103,13 @@ module.exports = Backbone.View.extend({
93103
}
94104
},
95105

106+
addUsers: function(model) {
107+
//add more users to the collection
108+
console.log("add users")
109+
this.usersShort.add(model);
110+
this.renderUserSet(this.nextUserToShow, this.nextUserToShow + this.showPerScroll);
111+
},
112+
96113
renderNoneFound: function(){
97114
var simpleMessage = new simpleMessageView({title: this.options.title, message: this.options.message, el: this.$el});
98115
this.subViews.push(simpleMessage);

js/views/userPageVw.js

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,10 @@ module.exports = baseVw.extend({
664664
if(self.options.ownPage === false && Boolean(__.findWhere(followingArray, {guid: self.userID}))){
665665
self.$('.js-followsMe').removeClass('hide')
666666
}
667+
//mark whether page is being followed
668+
if(self.options.ownPage === false){
669+
self.toggleFollowButtons(Boolean(__.findWhere(followingArray, {guid: self.pageID})));
670+
}
667671

668672
}).fail(function(jqXHR, status, errorThrown){
669673
if (self.isRemoved()) return;
@@ -690,27 +694,26 @@ module.exports = baseVw.extend({
690694
var self = this,
691695
fetchFollowersParameters;
692696

693-
if(this.ownPage){
697+
if(this.options.ownPage){
694698
fetchFollowersParameters = $.param({'start': this.followerFetchStart});
695699
} else {
696700
fetchFollowersParameters = $.param({'guid': this.pageID, 'start': this.followerFetchStart});
697701
}
698702

703+
console.log("fetchFollowers")
704+
699705
this.followers.fetch({
700-
data: self.userProfileFetchParameters,
706+
data: fetchFollowersParameters,
701707
//timeout: 5000,
702708
success: (model)=> {
703-
var followerArray = model.get('followers');
704-
this.$('.js-userFollowerCount').html(model.get('count'));
705-
this.followerFetchStart += model.length;
709+
var followerArray = model.get('followers'),
710+
followerCount = model.get('count') || followerArray.length;
711+
this.$('.js-userFollowerCount').html(followerCount);
712+
this.followerFetchStart += followerArray.length;
706713

707714
if (self.isRemoved()) return;
708715

709-
this.renderFollowers(followerArray);
710-
//if this is not their page, see if they are being followed
711-
if(this.options.ownPage === false){
712-
this.toggleFollowButtons(Boolean(__.findWhere(followerArray, {guid: this.userID})));
713-
}
716+
this.renderFollowers(followerArray, followerCount);
714717
},
715718
error: function(model, response){
716719
if (self.isRemoved()) return;
@@ -793,22 +796,27 @@ module.exports = baseVw.extend({
793796
}
794797
},
795798

796-
renderFollowers: function (model) {
799+
renderFollowers: function (model, followerCount) {
797800
"use strict";
798801

799802
model = model || [];
800-
this.followerList = new personListView({
801-
model: model,
802-
el: '.js-list1',
803-
title: window.polyglot.t('NoFollowers'),
804-
message: "",
805-
ownFollowing: this.ownFollowing,
806-
hideFollow: true,
807-
serverUrl: this.options.userModel.get('serverUrl'),
808-
reverse: true,
809-
perFetch: 30
810-
});
811-
this.registerChild(this.followerList);
803+
if(!this.followerList) {
804+
this.followerList = new personListView({
805+
model: model,
806+
el: '.js-list1',
807+
title: window.polyglot.t('NoFollowers'),
808+
message: "",
809+
ownFollowing: this.ownFollowing,
810+
hideFollow: true,
811+
serverUrl: this.options.userModel.get('serverUrl'),
812+
reverse: true,
813+
perFetch: 30,
814+
followerCount: followerCount
815+
});
816+
this.registerChild(this.followerList);
817+
}else{
818+
this.followerList.addUsers(model);
819+
}
812820

813821
if (model.length) {
814822
this.followersSearch = new window.List('searchFollowers', {
@@ -822,6 +830,11 @@ module.exports = baseVw.extend({
822830
this.followersSearch.reIndex();
823831
searchTerms && this.followersSearch.search(searchTerms);
824832
});
833+
834+
this.listenTo(this.followerList, 'fetchMoreUsers', ()=>{
835+
console.log("trigger fetch");
836+
this.fetchFollowers();
837+
});
825838
},
826839

827840
renderFollowing: function (model) {

0 commit comments

Comments
 (0)