Currently, I have a list of items with like and dislike buttons attached to each one. My goal is to organize this list based on the total score, which is calculated by subtracting the number of dislikes from the number of likes.
I am facing some challenges and questions related to this process:
- Why is the handlebar + sort method not functioning properly on the client side, and how can I fix it?
- Would a server-side solution be more efficient, considering that it may take up unnecessary disk space and be seen as premature optimization?
In my collections file, specifically in items.js, I store the item information in the following manner:
var item = _.extend(itemAttributes, {
lovers: [],
likes: 0,
haters: [],
dislikes: 0
//popularity: likes - dislikes I tried inserting in collection but doesnt work too
});
To sort the items, I am using a handlebar helper along with an extension of the main list controller.
Handlebar segment:
Template.registerHelper('popularity', function(likes, dislikes) {
var popularity = likes - dislikes;
return popularity;
Sorting logic in router.js:
BestItemController = ItemListController.extend({
sort: {popularity: -1},
nextPath: function() {
return Router.routes.BestItem.path({itemsLimit: this.itemsLimit() + this.increment})
}
});
The handlebar calculations for popularity seem to be working fine in displaying the popularity score, however, the actual sorting does not seem to be functioning correctly. It appears that the sorting operation is based on the original item fields rather than the calculated popularity score.
I have also attempted to add an additional schema field (as mentioned in the commented line), but encountered errors stating that the 'likes' are not defined.
If anyone could offer guidance or suggest alternative methods for improving this process, it would be greatly appreciated. For example, perhaps sorting could be done within the individual template helper files instead of the main router.js files.
Thank you in advance for any assistance!