simplifying and refactoring app.js code

This commit is contained in:
Sacha Greif 2013-04-05 12:41:50 +09:00
parent dbc589a8bc
commit 50c8867240

View file

@ -66,8 +66,8 @@ STATUS_PENDING=1;
STATUS_APPROVED=2; STATUS_APPROVED=2;
STATUS_REJECTED=3; STATUS_REJECTED=3;
var queryFind = function(status, slug){ var findPosts = function(status){
console.log('calling queryFind') console.log('calling findPosts')
var find = {}; var find = {};
// build find query starting with the status // build find query starting with the status
@ -90,8 +90,7 @@ var queryFind = function(status, slug){
// if a category slug is defined, modify selector // if a category slug is defined, modify selector
// DOESN'T WORK FOR SOME REASON if(slug = Session.get('categorySlug')){
if(slug){
console.log('category slug: ', slug); console.log('category slug: ', slug);
find={ find={
$and : [ $and : [
@ -104,10 +103,10 @@ var queryFind = function(status, slug){
return find; return find;
} }
var sortBy = function(sortProperty){ var sortPosts = function(sortProperty){
var sortBy = {sort: {sticky: -1}}; var sort = {sort: {sticky: -1}};
sortBy.sort[sortProperty] = -1; sort.sort[sortProperty] = -1;
return sortBy; return sort;
} }
var postListSubscription = function(find, options, per_page) { var postListSubscription = function(find, options, per_page) {
@ -119,19 +118,10 @@ var postListSubscription = function(find, options, per_page) {
return handle; return handle;
} }
topPostsHandle = postListSubscription(findPosts(STATUS_APPROVED), sortPosts('score'), 10);
var FIND_APPROVED = function() { newPostsHandle = postListSubscription(findPosts(STATUS_APPROVED), sortPosts('submitted'), 10);
console.log('calling FIND_APPROVED'); bestPostsHandle = postListSubscription(findPosts(STATUS_APPROVED), sortPosts('baseScore'), 10);
return queryFind(STATUS_APPROVED, Session.get('categorySlug')); pendingPostsHandle = postListSubscription(findPosts(STATUS_PENDING), sortPosts('createdAt'), 10);
}
var FIND_PENDING = function() {
return queryFind(STATUS_PENDING, Session.get('categorySlug'));
}
topPostsHandle = postListSubscription(FIND_APPROVED, sortBy('score'), 10);
newPostsHandle = postListSubscription(FIND_APPROVED, sortBy('submitted'), 10);
bestPostsHandle = postListSubscription(FIND_APPROVED, sortBy('baseScore'), 10);
pendingPostsHandle = postListSubscription(FIND_PENDING, sortBy('createdAt'), 10);
// digest subscriptions // digest subscriptions
DIGEST_PRELOADING = 3; DIGEST_PRELOADING = 3;
@ -156,7 +146,7 @@ Meteor.autorun(function() {
$gte: mDate.startOf('day').valueOf(), $gte: mDate.startOf('day').valueOf(),
$lt: mDate.endOf('day').valueOf() $lt: mDate.endOf('day').valueOf()
} }
}, FIND_APPROVED); }, findPosts(STATUS_APPROVED));
// note: the digest is ranked by baseScore and not score because we want the posts with the most votes of the day // note: the digest is ranked by baseScore and not score because we want the posts with the most votes of the day
// independantly of age // independantly of age
var options = {sort: {baseScore: -1}}; var options = {sort: {baseScore: -1}};