Skip to content

Instantly share code, notes, and snippets.

@nilsi
Last active March 25, 2016 03:17
Show Gist options
  • Save nilsi/2eee6c3a37aaf37ac701 to your computer and use it in GitHub Desktop.
Save nilsi/2eee6c3a37aaf37ac701 to your computer and use it in GitHub Desktop.
Same code on Client and Server confusion.
if (Meteor.isClient()) {
Template.product_carousel.onRendered(function () {
self.autorun(function() {
self.subscribe('relatedProducts');
});
});
Template.product_carousel.helpers({
similarProducts: function () {
var product = Session.get("expandedProduct");
var products, fillProducts;
var category, id, type;
if (product) {
type = product.type;
category = product.category;
id = product._id;
}
products = Products.find({
_id: { $ne: id },
type: type
},
{
sort: {createdOn: -1},
limit: 15
}).fetch();
if (products.length < 15) {
var limit = 15 - products.length;
fillProducts = Products.find({
_id: { $nin: _.pluck(products, "_id"),
$ne: id },
category: category,
},
{
sort: {createdOn: -1},
limit: limit
}).fetch();
}
steps = 0; /* resetting autoscroll */
return products.concat(fillProducts);
}
});
}
if (Meteor.isServer()) {
Meteor.publish('relatedProducts', function() {
check(username, String);
check(limit, Number);
var product = Session.get("expandedProduct");
var products, fillProducts;
var category, id, type;
if (product) {
type = product.type;
category = product.category;
id = product._id;
}
products = Products.find({
_id: { $ne: id },
type: type
},
{
sort: {createdOn: -1},
limit: 15
}).fetch();
if (products.length < 15) {
var limit = 15 - products.length;
fillProducts = Products.find({
_id: { $nin: _.pluck(products, "_id"),
$ne: id },
category: category,
},
{
sort: {createdOn: -1},
limit: limit
}).fetch();
}
steps = 0; /* resetting autoscroll */
return products.concat(fillProducts);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment