From 5bf2e7896e23736dd37f1ee34982fb4fbb9a6173 Mon Sep 17 00:00:00 2001 From: erasaur Date: Mon, 27 Apr 2015 22:04:48 -0700 Subject: [PATCH] WIP: move away from global sessions for items limit and infinite scrolling on home page #9, handle subs with controller state #6 --- client/views/common/infinite_scroll.js | 14 +++++++++----- client/views/home/home.js | 13 +++++++------ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/client/views/common/infinite_scroll.js b/client/views/common/infinite_scroll.js index 128188e..17614dd 100644 --- a/client/views/common/infinite_scroll.js +++ b/client/views/common/infinite_scroll.js @@ -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 || []; @@ -34,12 +37,13 @@ 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 () { @@ -47,4 +51,4 @@ stopInfiniteScroll = function () { _.each(this._infiniteScroll, function (obj) { obj.stop(); }); -}; \ No newline at end of file +}; diff --git a/client/views/home/home.js b/client/views/home/home.js index f22619d..2d460f7 100644 --- a/client/views/home/home.js +++ b/client/views/home/home.js @@ -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'); } });