mirror of
https://github.com/vale981/Vulcan
synced 2025-03-08 19:11:38 -05:00
refactor template-level subs pattern
This commit is contained in:
parent
19cf05b77f
commit
c266c91a17
1 changed files with 39 additions and 74 deletions
|
@ -2,89 +2,54 @@
|
|||
|
||||
/*
|
||||
|
||||
This template acts as the controller that sets and manages the reactive context
|
||||
for the embedded postsList template. It receives its parameters from a "caller" template.
|
||||
This template acts as the controller that sets and manages the reactive data context
|
||||
for the embedded postsList template.
|
||||
|
||||
It doesn't care if it receives its context from the router or from another template.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
Template.postsListController.onCreated(function () {
|
||||
|
||||
// 1. Initialization (*not* reactive!)
|
||||
var instance = this;
|
||||
var terms = instance.data.terms; // provided by the caller template
|
||||
|
||||
// if a limit is provided by the data context use this, else use default
|
||||
var limit = !!instance.data.terms.limit ? instance.data.terms.limit : Settings.get('postsPerPage', 10);
|
||||
|
||||
// if terms doesn't have a limit, set one
|
||||
if (!terms.limit) {
|
||||
terms.limit = Settings.get('postsPerPage', 10);
|
||||
}
|
||||
|
||||
// initialize the reactive variables
|
||||
instance.postsLoaded = new ReactiveVar(0);
|
||||
instance.terms = new ReactiveVar(terms);
|
||||
|
||||
// 2. Autorun
|
||||
|
||||
// will re-run when terms are changed, either by the router or by the template itself
|
||||
instance.autorun(function () {
|
||||
|
||||
// console.log('// ⚡ autorun ------------------------');
|
||||
|
||||
// ⚡ reactive variables ⚡
|
||||
var routerTerms = Router.current().data().terms; // get terms from router (category, date, etc.)
|
||||
var instanceTerms = instance.terms.get(); // get terms from instance (posts limit)
|
||||
|
||||
var terms = _.extend(instanceTerms, routerTerms); // merge both
|
||||
|
||||
var subsReady = instance.subscriptionsReady();
|
||||
|
||||
// console.log("> Asking for " + terms.limit + " posts…");
|
||||
|
||||
// subscribe
|
||||
var postsSubscription = instance.subscribe('postsList', terms);
|
||||
var usersSubscription = instance.subscribe('postsListUsers', terms);
|
||||
|
||||
// if subscriptions are ready, set limit to newLimit
|
||||
if (subsReady) {
|
||||
|
||||
// console.log("> Received "+terms.limit+" posts.");
|
||||
instance.postsLoaded.set(terms.limit);
|
||||
|
||||
} else {
|
||||
// console.log("> Subscriptions are not ready yet.");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// 3. Cursor
|
||||
|
||||
instance.getPostsCursor = function() {
|
||||
|
||||
// console.log('// cursor ----------')
|
||||
// console.log('> actually loaded ' + instance.postsLoaded.get() + ' posts')
|
||||
|
||||
var termsGet = _.clone(instance.terms.get());
|
||||
var termsLoaded = _.extend(termsGet, {limit: instance.postsLoaded.get()});
|
||||
var parameters = Posts.getSubParams(termsLoaded);
|
||||
|
||||
return Posts.find(parameters.find, parameters.options);
|
||||
};
|
||||
// initialize postsLimit template instance reactive variable
|
||||
instance.postsLimit = new ReactiveVar(limit);
|
||||
|
||||
});
|
||||
|
||||
Template.postsListController.helpers({
|
||||
context: function () {
|
||||
|
||||
// console.log('// context ---------')
|
||||
|
||||
// create context for postsList module
|
||||
var instance = Template.instance();
|
||||
|
||||
// ⚡ reactive variables ⚡
|
||||
var terms = instance.terms.get();
|
||||
var postsCursor = instance.getPostsCursor();
|
||||
var postsReady = instance.subscriptionsReady();
|
||||
// get terms from data context
|
||||
var terms = this.terms; // ⚡ reactive ⚡
|
||||
|
||||
// get limit from template instance reactive variable
|
||||
var postsLimit = instance.postsLimit.get(); // ⚡ reactive ⚡
|
||||
|
||||
// use limit to get subscriptions terms, then subscribe
|
||||
var subscriptionTerms = _.extend(_.clone(terms), {limit: postsLimit}); // extend terms with limit
|
||||
var postsSubscription = instance.subscribe('postsList', subscriptionTerms);
|
||||
var usersSubscription = instance.subscribe('postsListUsers', subscriptionTerms);
|
||||
var subscriptionsReady = instance.subscriptionsReady(); // ⚡ reactive ⚡
|
||||
|
||||
// if subscriptions are ready, update query terms to match the ones we subscribed with
|
||||
if (subscriptionsReady) {
|
||||
terms = subscriptionTerms;
|
||||
}
|
||||
|
||||
// console.log('// ------ context running ------ //');
|
||||
// console.log("terms: ", terms);
|
||||
// console.log("limit: ", postsLimit);
|
||||
// console.log("ready: ", subscriptionsReady);
|
||||
|
||||
var parameters = Posts.getSubParams(terms);
|
||||
var postsCursor = Posts.find(parameters.find, parameters.options);
|
||||
|
||||
var context = {
|
||||
|
||||
|
@ -92,19 +57,19 @@ Template.postsListController.helpers({
|
|||
postsCursor: postsCursor,
|
||||
|
||||
// posts subscription readiness, used to show spinner
|
||||
postsReady: postsReady,
|
||||
postsReady: subscriptionsReady,
|
||||
|
||||
// whether to show the load more button or not
|
||||
hasMorePosts: postsCursor.count() >= terms.limit,
|
||||
hasMorePosts: postsCursor.count() >= postsLimit,
|
||||
|
||||
// what to do when user clicks "load more"
|
||||
loadMoreHandler: function (instance) {
|
||||
event.preventDefault();
|
||||
|
||||
// increase limit by 5 and update terms
|
||||
terms.limit += Settings.get('postsPerPage', 10);
|
||||
instance.terms.set(terms);
|
||||
|
||||
var limit = instance.postsLimit.get();
|
||||
// increase limit by 5 and update instance variable
|
||||
limit += Settings.get('postsPerPage', 10);
|
||||
instance.postsLimit.set(limit);
|
||||
//TODO: also update URL
|
||||
},
|
||||
|
||||
// the current instance
|
||||
|
|
Loading…
Add table
Reference in a new issue