Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 9d5b835

Browse files
committed
another big refactor
1 parent e9b5819 commit 9d5b835

2 files changed

Lines changed: 49 additions & 101 deletions

File tree

packages/telescope-lib/package.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Package.onUse(function (api) {
6767
// 'utilities:state-transitions@0.1.0',
6868
'tmeasday:publish-counts@0.7.1',
6969
// 'dburles:iron-router-query-array@1.0.1'
70-
'utilities:onsubscribed@0.1.0'
70+
'utilities:onsubscribed@0.1.1'
7171
];
7272

7373
api.use(packages);

packages/telescope-posts/lib/client/templates/posts_list/posts_list_controller.js

Lines changed: 48 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// see https://www.discovermeteor.com/blog/template-level-subscriptions/
22

3-
var debug = false;
4-
53
/*
64
75
This template acts as the controller that sets and manages the reactive context
@@ -21,14 +19,11 @@ Template.posts_list_controller.onCreated(function () {
2119
// 1. Initialization (*not* reactive!)
2220
var template = this;
2321
var terms = _.clone(template.data.terms);
24-
var limit = Settings.get('postsPerPage', 10);
25-
var postsSubscription;
26-
var usersSubscription;
27-
var subscriptionTerms;
28-
22+
template.terms = terms;
23+
2924
// initialize the reactive variables
3025
template.rTerms = new ReactiveVar(terms);
31-
template.rLimit = new ReactiveVar(limit);
26+
template.rLimit = new ReactiveVar(Settings.get('postsPerPage', 10));
3227
template.rReady = new ReactiveVar(false);
3328

3429
// if caching is set to true, use Subs Manager. Else use template.subscribe. Default to false
@@ -38,113 +33,66 @@ Template.posts_list_controller.onCreated(function () {
3833
// enable not subscribing to users on a per-controller basis
3934
var subscribeToUsers = (typeof terms.subscribeToUsers === "undefined") ? true : terms.subscribeToUsers;
4035

41-
// Autorun 1: reruns when Template.currentData().terms changes (and new terms are different from old ones),
42-
// or when post limit changes. On first run, terms and limit won't have had time to change yet, so just
43-
// do nothing
44-
template.autorun(function (computation) {
36+
template.autorun(function () {
4537

38+
// set controller as not ready
39+
template.rReady.set(false);
4640

47-
// add a dependency on data context to trigger the autorun
48-
var newTerms = Template.currentData().terms; // ⚡ reactive ⚡
41+
// depend on terms
42+
var newTerms = _.clone(Template.currentData().terms); // ⚡ reactive ⚡
4943

50-
// get limit from local template variable
44+
// depend on rLimit
5145
var rLimit = template.rLimit.get(); // ⚡ reactive ⚡
52-
53-
if (!computation.firstRun) {
54-
55-
if (debug) {
56-
console.log('// ------ autorun 1 ------ //');
57-
console.log("terms: ", _.clone(terms));
58-
console.log("newTerms: ", _.clone(newTerms));
59-
console.log("isEqual?: ", _.isEqual(newTerms, terms));
60-
console.log("oldLimit: ", limit);
61-
console.log("newLimit: ", rLimit);
62-
}
63-
64-
if (!_.isEqual(newTerms, terms)) {
65-
// Case 1: terms have changed
66-
// Note: if terms have changed, we reset the limit no matter its value
67-
68-
// update terms
69-
terms = newTerms;
70-
71-
// reset limit
72-
limit = Settings.get('postsPerPage', 10);
73-
template.rLimit.set(limit);
74-
75-
} else if (rLimit !== limit) {
76-
// Case 2: limit has changed
7746

78-
// update limit
79-
limit = rLimit;
80-
81-
}
47+
// complete terms with rLimit
48+
newTerms.limit = rLimit;
8249

83-
// set template.rReady to false to trigger Autorun 2
84-
template.rReady.set(false);
50+
// subscribe to posts and (optionally) users
51+
var postsSubscription = subscriber.subscribe('postsList', newTerms);
52+
if (subscribeToUsers) {
53+
var usersSubscription = subscriber.subscribe('postsListUsers', newTerms);
8554
}
8655

87-
});
56+
// check if subscriptions are ready
57+
var subscriptionsReady;
58+
if (subscribeToUsers) {
59+
subscriptionsReady = postsSubscription.ready() && usersSubscription.ready(); // ⚡ reactive ⚡
60+
} else {
61+
subscriptionsReady = postsSubscription.ready(); // ⚡ reactive ⚡
62+
}
8863

89-
// Autorun 2: runs once on load, then reruns when template.rReady changes
90-
// On first run, we subscribe with the original terms andlimit
91-
template.autorun(function () {
64+
// console.log('// ------ autorun ------ //');
65+
// console.log("newTerms: ", newTerms);
66+
// console.log("rLimit: ", rLimit);
67+
// console.log("subscriptionsReady: ", subscriptionsReady);
9268

93-
var ready = template.rReady.get(); // ⚡ reactive ⚡
94-
95-
// if ready is false, (re)subscribe
96-
if (!ready) {
97-
98-
subscriptionTerms = _.clone(terms);
99-
subscriptionTerms.limit = limit;
100-
101-
if (debug) {
102-
console.log('// ------ autorun 2 ------ //');
103-
console.log("subscriptionTerms: ", subscriptionTerms);
104-
console.log("template.rReady: ", ready);
105-
}
106-
107-
// subscribe to posts and (optionally) users
108-
postsSubscription = subscriber.subscribe('postsList', subscriptionTerms);
109-
if (subscribeToUsers) {
110-
usersSubscription = subscriber.subscribe('postsListUsers', subscriptionTerms);
111-
}
112-
113-
// Autorun 3: when subscription is ready, update the data helper's terms
114-
// On the first run, the subscription won't be ready yet so there is no need to update the terms
115-
// Note: this needs to be nested inside Autorun 2 because it needs to re-run even when
116-
// subscription.ready() is already ready (because of subscription caching)
117-
Tracker.autorun(function (computation) {
118-
119-
var subscriptionsReady;
120-
121-
if (subscribeToUsers) {
122-
subscriptionsReady = postsSubscription.ready() && usersSubscription.ready(); // ⚡ reactive ⚡
123-
} else {
124-
subscriptionsReady = postsSubscription.ready(); // ⚡ reactive ⚡
125-
}
126-
127-
// if (!computation.firstRun) {
128-
129-
if (debug) {
130-
console.log('// ------ autorun 3 ------ //');
131-
console.log("subscriptionsReady: ", subscriptionsReady);
132-
}
133-
134-
// if subscriptions are ready, set terms to subscriptionsTerms
135-
if (subscriptionsReady) {
136-
template.rTerms.set(subscriptionTerms);
137-
template.rReady.set(true);
138-
}
139-
140-
// }
141-
});
69+
// if subscriptions are ready, set terms to newTerms
70+
if (subscriptionsReady) {
71+
template.rTerms.set(newTerms);
72+
template.rReady.set(true);
14273
}
14374

14475
});
14576

146-
77+
});
78+
79+
// runs whenever the template's data context changes to reset rLimit
80+
Template.posts_list_controller.onDataChanged(function (data) {
14781

82+
var template = this;
83+
var oldTerms = template.terms;
84+
var newTerms = data.terms;
85+
86+
// console.log("// ------ onDataChanged ------ //")
87+
// console.log("oldTerms: ", _.clone(oldTerms));
88+
// console.log("newTerms: ", _.clone(newTerms));
89+
// console.log("isEqual?: ", _.isEqual(newTerms, oldTerms));
90+
91+
// if terms have changed, we reset rLimit
92+
if (!_.isEqual(oldTerms, newTerms)) {
93+
template.terms = newTerms;
94+
template.rLimit.set(Settings.get('postsPerPage', 10));
95+
}
14896
});
14997

15098
Template.posts_list_controller.helpers({

0 commit comments

Comments
 (0)