Vulcan/packages/telescope-singleday/lib/client/templates/single_day.js

121 lines
3.6 KiB
JavaScript
Raw Normal View History

// 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);
instance.getTerms = function () {
2015-01-08 17:26:58 +09:00
// if instance has a set date use this, else depend on Session variable
2015-01-08 17:42:56 +09:00
var currentDate = (!!instance.data && !!instance.data.date) ? instance.data.date: Session.get('currentDate');
return {
view: 'digest',
after: moment(currentDate).startOf('day').toDate(),
2015-01-08 16:39:05 +09:00
before: moment(currentDate).endOf('day').toDate()
};
};
// 2. Autorun
2015-01-09 16:05:00 +09:00
// this 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 16:39:05 +09:00
// will re-run when postsLimit or currentDate change
instance.autorun(function () {
var terms = instance.getTerms();
// get the postsLimit
terms.limit = instance.postsLimit.get();
2015-01-08 17:26:58 +09:00
// console.log("Asking for " + terms.limit + " posts…")
// subscribe
var postsSubscription = Meteor.subscribe('postsList', terms);
var usersSubscription = Meteor.subscribe('postsListUsers', terms);
// 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")
instance.postsLoaded.set(terms.limit);
instance.postsReady.set(true);
2015-01-08 17:26:58 +09:00
} else {
instance.postsReady.set(false);
2015-01-08 17:26:58 +09:00
// console.log("> Subscription is not ready yet. \n\n");
}
});
// 3. Cursor
instance.getPostsCursor = function() {
2015-01-09 16:05:00 +09:00
// console.log('loaded ' + instance.postsLoaded.get() + ' posts')
var termsLoaded = _.extend(instance.getTerms(), {limit: instance.postsLoaded.get()});
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 () {
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 () {
// 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"
loadMoreHandler: function (instance) {
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;
}
});