2015-01-08 15:38:07 +09:00
|
|
|
// see https://www.discovermeteor.com/blog/template-level-subscriptions/
|
|
|
|
|
|
|
|
// this template acts as the controller that sets and
|
|
|
|
// manages the reactive context for the embedded postsList template
|
|
|
|
|
|
|
|
Template[getTemplate('singleDay')].created = function () {
|
|
|
|
|
|
|
|
// 1. Initialization
|
|
|
|
|
|
|
|
var instance = this;
|
|
|
|
|
|
|
|
// initialize the reactive variables
|
|
|
|
instance.postsLoaded = new ReactiveVar(0);
|
|
|
|
instance.postsLimit = new ReactiveVar(getSetting('postsPerPage', 10));
|
|
|
|
instance.postsReady = new ReactiveVar(false);
|
|
|
|
|
2015-01-08 16:01:51 +09:00
|
|
|
instance.getTerms = function () {
|
2015-01-08 17:26:58 +09:00
|
|
|
// if instance has a set date use this, else depend on Session variable
|
|
|
|
var currentDate = !!instance.data.date ? instance.data.date: Session.get('currentDate');
|
2015-01-08 16:01:51 +09:00
|
|
|
return {
|
|
|
|
view: 'digest',
|
|
|
|
after: moment(currentDate).startOf('day').toDate(),
|
2015-01-08 16:39:05 +09:00
|
|
|
before: moment(currentDate).endOf('day').toDate()
|
2015-01-08 16:01:51 +09:00
|
|
|
}
|
2015-01-08 15:38:07 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
// 2. Autorun
|
|
|
|
|
2015-01-08 16:39:05 +09:00
|
|
|
// will re-run when postsLimit or currentDate change
|
2015-01-08 15:38:07 +09:00
|
|
|
instance.autorun(function () {
|
|
|
|
|
2015-01-08 16:01:51 +09:00
|
|
|
var terms = instance.getTerms();
|
|
|
|
|
2015-01-08 15:38:07 +09:00
|
|
|
// get the postsLimit
|
|
|
|
terms.limit = instance.postsLimit.get();
|
|
|
|
|
2015-01-08 17:26:58 +09:00
|
|
|
// console.log("Asking for " + terms.limit + " posts…")
|
2015-01-08 15:38:07 +09:00
|
|
|
|
|
|
|
// subscribe
|
2015-01-08 16:01:51 +09:00
|
|
|
var postsSubscription = Meteor.subscribe('postsList', terms);
|
|
|
|
var usersSubscription = Meteor.subscribe('postsListUsers', terms);
|
2015-01-08 15:38:07 +09:00
|
|
|
|
|
|
|
// if subscriptions are ready, set limit to newLimit
|
|
|
|
if (postsSubscription.ready() && usersSubscription.ready()) {
|
2015-01-08 17:26:58 +09:00
|
|
|
|
|
|
|
// console.log("> Received "+terms.limit+" posts. \n\n")
|
2015-01-08 15:38:07 +09:00
|
|
|
instance.postsLoaded.set(terms.limit);
|
|
|
|
instance.postsReady.set(true);
|
2015-01-08 17:26:58 +09:00
|
|
|
|
2015-01-08 15:38:07 +09:00
|
|
|
} else {
|
2015-01-08 17:26:58 +09:00
|
|
|
|
2015-01-08 15:38:07 +09:00
|
|
|
instance.postsReady.set(false);
|
2015-01-08 17:26:58 +09:00
|
|
|
// console.log("> Subscription is not ready yet. \n\n");
|
|
|
|
|
2015-01-08 15:38:07 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-01-08 16:39:05 +09:00
|
|
|
// this second autorun is there just to reset the post limit
|
|
|
|
// when current date changes (i.e. we're switching page)
|
|
|
|
instance.autorun(function () {
|
|
|
|
// just by including this session variable in the autorun, we automatically make it depend on it
|
|
|
|
var currentDate = Session.get('currentDate');
|
|
|
|
instance.postsLimit.set(getSetting('postsPerPage', 10));
|
|
|
|
});
|
|
|
|
|
2015-01-08 15:38:07 +09:00
|
|
|
// 3. Cursor
|
|
|
|
|
|
|
|
instance.getPostsCursor = function() {
|
2015-01-08 16:01:51 +09:00
|
|
|
var termsLoaded = _.extend(instance.getTerms(), {limit: instance.postsLoaded.get()});
|
2015-01-08 15:38:07 +09:00
|
|
|
var parameters = getPostsParameters(termsLoaded);
|
|
|
|
return Posts.find(parameters.find, parameters.options);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-06 14:46:38 +09:00
|
|
|
Template[getTemplate('singleDay')].helpers({
|
2015-01-08 17:26:58 +09:00
|
|
|
showDateNav: function () {
|
|
|
|
console.log(this)
|
|
|
|
return (typeof this.showDateNav === 'undefined') ? true : this.showDateNav;
|
|
|
|
},
|
2015-01-06 14:46:38 +09:00
|
|
|
singleDayNav: function () {
|
|
|
|
return getTemplate('singleDayNav');
|
|
|
|
},
|
|
|
|
posts_list: function () {
|
|
|
|
return getTemplate('posts_list');
|
|
|
|
},
|
|
|
|
context: function () {
|
2015-01-08 15:38:07 +09:00
|
|
|
// create context for postsList module
|
|
|
|
var instance = Template.instance();
|
|
|
|
var postsCursor = instance.getPostsCursor();
|
|
|
|
|
|
|
|
var context = {
|
|
|
|
|
|
|
|
// posts cursor
|
|
|
|
postsCursor: postsCursor,
|
|
|
|
|
|
|
|
// posts subscription readiness, used to show spinner
|
|
|
|
postsReady: instance.postsReady.get(),
|
|
|
|
|
|
|
|
// whether to show the load more button or not
|
|
|
|
hasMorePosts: postsCursor.count() >= instance.postsLimit.get(),
|
|
|
|
|
|
|
|
// what to do when user clicks "load more"
|
2015-01-08 17:07:17 +09:00
|
|
|
loadMoreHandler: function (instance) {
|
2015-01-08 15:38:07 +09:00
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
// get current value for limit, i.e. how many posts are currently displayed
|
|
|
|
var limit = instance.postsLimit.get();
|
|
|
|
// increase limit by 5 and update it
|
|
|
|
limit += getSetting('postsPerPage', 10);
|
|
|
|
instance.postsLimit.set(limit);
|
|
|
|
},
|
|
|
|
|
|
|
|
// the current instance
|
|
|
|
controllerInstance: instance
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-06 14:46:38 +09:00
|
|
|
return context;
|
|
|
|
}
|
|
|
|
});
|