fix user posts limit bug

This commit is contained in:
Sacha Greif 2014-12-09 13:16:34 +09:00
parent 6d817fc421
commit eacc891bca

View file

@ -3,15 +3,21 @@ Template[getTemplate('userPosts')].created = function () {
var user = this.data;
var instance = this;
// initialize the terms and posts local reactive variables
// initialize the terms reactive variable
instance.terms = new ReactiveVar({
view: 'userPosts',
userId: user._id,
limit: 5
limit: 5,
previousLimit: 0 // should always be equal to limit's previous value
});
// initialize the posts cursor ReVar
instance.posts = new ReactiveVar({});
// will re-run when the "terms" local reactive variable changes
// initialize the subscription handle ReVar
instance.subscription = new ReactiveVar({});
// will re-run when the "terms" or "subscription" reactive variables change
Tracker.autorun(function () {
// get the new terms and generate new parameters from them
@ -19,11 +25,15 @@ Template[getTemplate('userPosts')].created = function () {
var parameters = getPostsParameters(terms);
// subscribe to the userPosts publication
instance.subscription = Meteor.subscribe('userPosts', terms);
instance.subscription.set(Meteor.subscribe('userPosts', terms));
// until subscription is ready, overwrite limit to restrict number of posts to previousLimit
if (!instance.subscription.get().ready())
parameters.options.limit = terms.previousLimit
// update the instance's "posts" cursor
instance.posts.set(Posts.find(parameters.find, parameters.options));
});
};
@ -32,10 +42,10 @@ Template[getTemplate('userPosts')].helpers({
return Template.instance().posts.get();
},
isReady: function () {
return Template.instance().subscription.ready();
return Template.instance().subscription.get().ready();
},
hasMorePosts: function () {
return Template.instance().posts.get().count() >= Template.instance().terms.get().limit;
return Template.instance().posts.get().count() >= Template.instance().terms.get().previousLimit;
}
});
@ -43,6 +53,8 @@ Template[getTemplate('userPosts')].events({
'click .posts-more': function (e) {
e.preventDefault();
var terms = Template.instance().terms.get();
// previousLimit starts at 0, so by increasing both values by 5 we keep them 5 apart
terms.previousLimit += 5;
terms.limit += 5;
Template.instance().terms.set(terms)
}