Skip to content

Commit

Permalink
WIP: move away from global sessions for items limit and infinite scro…
Browse files Browse the repository at this point in the history
…lling on home page #9, handle subs with controller state #6
  • Loading branch information
erasaur committed Apr 28, 2015
1 parent fc39892 commit 5bf2e78
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
14 changes: 9 additions & 5 deletions client/views/common/infinite_scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ function InfiniteScroll (cursor) {
}

initInfiniteScroll = function (cursors) {
var cursors = _.isArray(cursors) ? cursors : [cursors];
var self = this;
var cursors = _.isArray(cursors) ? cursors : [cursors];
var controller = getCurrentController();
var limit = this.state || controller.state;
var currentLimit;

self._infiniteScroll = self._infiniteScroll || [];

Expand All @@ -34,17 +37,18 @@ initInfiniteScroll = function (cursors) {

if (window.innerHeight + window.scrollY >= target) {
_.each(self._infiniteScroll, function (obj) {
if (obj.count >= Session.get('itemsLimit')) {
Session.set('itemsLimit', Session.get('itemsLimit') + 30); //fetch more items from server
currentLimit = limit.get('itemsLimit');
if (obj.count >= currentLimit) {
limit.set('itemsLimit', currentLimit + 30); //fetch more items from server
}
});
}
}, 300));
}, 300));
};

stopInfiniteScroll = function () {
$(window).off('scroll');
_.each(this._infiniteScroll, function (obj) {
obj.stop();
});
};
};
13 changes: 7 additions & 6 deletions client/views/home/home.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
Template.home.rendered = function () {
initInfiniteScroll.call(this, Topics.find());
};
Template.home.onCreated(function () {
initInfiniteScroll.call(this, Topics.find({}, { fields: { '_id': 1 } }));
});

Template.home.destroyed = function () {
Template.home.onDestroyed(function () {
stopInfiniteScroll.call(this);
};
});

Template.home.helpers({
topics: function() {
return Topics.find({}, { sort: { 'score': -1, 'createdAt': -1 } });
},
moreTopics: function () {
return Topics.find().count() === Session.get('itemsLimit');
var controller = getCurrentController();
return Topics.find({}, { fields: { '_id': 1 } }).count() === controller.state.get('itemsLimit');
}
});

0 comments on commit 5bf2e78

Please sign in to comment.