Skip to content
Browse files

cleaning up subscriptions

  • Loading branch information...
1 parent 8906d96 commit e622c11286180fb6a22b6f5e059cc4fd9036ef39 @SachaG committed
Showing with 44 additions and 47 deletions.
  1. +8 −29 client/main.js
  2. +2 −2 lib/analytics.js
  3. +0 −2 lib/permissions.js
  4. +12 −13 lib/router.js
  5. +21 −0 license.md
  6. +1 −1 server/publications.js
View
37 client/main.js
@@ -3,33 +3,13 @@ Session.set('initialLoad', true);
Session.set('today', new Date());
Session.set('view', 'top');
Session.set('postsLimit', getSetting('postsPerPage', 10));
-Session.set('settingsLoaded', false);
Session.set('sessionId', Meteor.default_connection._lastSessionId);
-// Subscriptions
-
-// note: here we only subscribe to subscriptions that we need to be available all the time.
-// For subscriptions depending on specific pages, see the router.
-
-// TODO: add session variable that tracks when all global subscriptions have loaded
-
-// Settings
-Meteor.subscribe('settings', function(){
- // runs once after settings have loaded
- Session.set('settingsLoaded',true);
- analyticsInit();
-});
-
-// Categories
-Meteor.subscribe('categories');
+STATUS_PENDING=1;
+STATUS_APPROVED=2;
+STATUS_REJECTED=3;
-// Current User
-// We need to subscribe to the currentUser subscription because by itself,
-// Meteor doesn't send all the user properties that we need
-Meteor.subscribe('currentUser');
-// Subscribe to all users for now to make user selection autocomplete work
-Meteor.subscribe('allUsersAdmin');
// Notifications - only load if user is logged in
// Not mandatory, because server won't publish anything even if we try to load.
@@ -38,10 +18,9 @@ Deps.autorun(function() {
// userId() can be changed before user(), because loading profile takes time
if(Meteor.userId()) {
Meteor.subscribe('notifications');
+ if(isAdmin(Meteor.user())){
+ // Subscribe to all users for now to make user selection autocomplete work
+ Meteor.subscribe('allUsersAdmin');
+ }
}
-})
-
-STATUS_PENDING=1;
-STATUS_APPROVED=2;
-STATUS_REJECTED=3;
-
+});
View
4 lib/analytics.js
@@ -1,4 +1,4 @@
-analyticsInit = function() {
+analyticsInit = _.once(function() {
// Mixpanel
if (mixpanelId=getSetting("mixpanelId")){
@@ -69,7 +69,7 @@ analyticsInit = function() {
}
-}
+});
analyticsRequest = function() {
View
2 lib/permissions.js
@@ -41,8 +41,6 @@ canPost = function(user, returnError){
var user=(typeof user === 'undefined') ? Meteor.user() : user;
// console.log('canPost', user, action, getSetting('requirePostInvite'));
- if(Meteor.isClient && !Session.get('settingsLoaded'))
- return false;
if(!user){
return returnError ? "no_account" : false;
View
25 lib/router.js
@@ -85,18 +85,23 @@ RSS
*/
+// uncomment to disable FastRender
+// var FastRender = {RouteController: RouteController, onAllRoutes: function() {}};
//--------------------------------------------------------------------------------------------------//
//--------------------------------------------- Config ---------------------------------------------//
//--------------------------------------------------------------------------------------------------//
+var preloadSubscriptions = ['categories', 'settings', 'currentUser'];
Router.configure({
layoutTemplate: 'layout',
loadingTemplate: 'loading',
notFoundTemplate: 'not_found',
- waitOn: function() {
- return Meteor.subscribe('categories');
+ waitOn: function () {
+ return _.map(preloadSubscriptions, function(sub){
+ Meteor.subscribe(sub);
+ });
}
});
@@ -137,7 +142,6 @@ var filters = {
},
isAdmin: function() {
- this.subscribe('currentUser').wait();
if(!this.ready()) return;
if(!isAdmin()){
throwError(i18n.t("Sorry, you have to be an admin to view this page."))
@@ -147,8 +151,6 @@ var filters = {
},
canView: function() {
- this.subscribe('currentUser').wait();
- this.subscribe('settings').wait();
if(!this.ready()) return;
if(!canView()){
console.log('cannot view')
@@ -158,8 +160,6 @@ var filters = {
},
canPost: function () {
- this.subscribe('currentUser').wait();
- this.subscribe('settings').wait();
if(!this.ready()) return;
if(!canPost()){
throwError(i18n.t("Sorry, you don't have permissions to add new items."))
@@ -169,7 +169,6 @@ var filters = {
},
canEditPost: function() {
- this.subscribe('currentUser').wait();
if(!this.ready()) return;
// Already subscribed to this post by route({waitOn: ...})
var post = Posts.findOne(this.params._id);
@@ -181,7 +180,6 @@ var filters = {
},
canEditComment: function() {
- this.subscribe('currentUser').wait();
if(!this.ready()) return;
// Already subscribed to this commit by CommentPageController
var comment = Comments.findOne(this.params._id);
@@ -193,7 +191,6 @@ var filters = {
},
hasCompletedProfile: function() {
- this.subscribe('currentUser').wait();
if(!this.ready()) return;
var user = Meteor.user();
if (user && ! userProfileComplete(user)){
@@ -255,6 +252,7 @@ if(Meteor.isClient){
Router.after(filters.resetScroll, {except:['posts_top', 'posts_new', 'posts_best', 'posts_pending', 'posts_category', 'all-users']});
Router.after( function () {
+ analyticsInit(); // will only run once thanks to _.once()
analyticsRequest() // log this request with mixpanel, etc
});
@@ -704,8 +702,9 @@ Router.map(function() {
// it might have considerable amount of docs
if(Meteor.isServer) {
FastRender.onAllRoutes(function() {
- this.subscribe('categories');
- this.subscribe('settings');
- this.subscribe('currentUser');
+ var router = this;
+ _.each(preloadSubscriptions, function(sub){
+ router.subscribe(sub);
+ });
});
}
View
21 license.md
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) [year] [fullname]
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
View
2 server/publications.js
@@ -17,7 +17,7 @@ var privacyOptions = { // false means private
// Publish the current user
Meteor.publish('currentUser', function() {
- var user = Meteor.users.find(this.userId);
+ var user = Meteor.users.find({_id: this.userId});
return user;
});

0 comments on commit e622c11

Please sign in to comment.
Something went wrong with that request. Please try again.