I hope all of you know the pinterest style layout and its behaviour:
Bonus task:
I am thinking how to program that logic in Meteor way. Let's skip user interface code, I am interested only in business logic.
I am thinking about ProductsDisplayed collection as helper, which is empty and populated by 20 products on page load, then when scrolling point is reached, I insert 20 products more from original Products collection etc.
Problems:
But maybe the whole idea about ProductsDisplayed is wrong. I would love to know what do you think!
Update!
I changed approach to using only Products collection.
server:
Meteor.publish "productsDisplayed", (number) ->
Products.find {},
limit: number
client:
Meteor.autosubscribe ->
Meteor.subscribe "productsDisplayed", Session.get('productsDisplayedNumber')
and incrementing by 20 Session 'productsDisplayedNumber' when scrolling point reached. But autosubscribe makes that the whole template is rerendered, not only the new elements. Any ideas?
I finally solved this problem. The solution is to have client only Collection, like that:
# on client
# client side only collection
ProductsDisplayed = new Meteor.Collection(null)
then when scrolling point is reached ask server for next N (limitNo
) products
# on client
Meteor.call 'getProducts', limitNo, skipNo, (err, products) =>
_.each products, (item, index) =>
# get rid of _id
delete item._id
ProductsDisplayed.insert( item )
skipNo
is incremented by N, to always ask for next set of data. And on server side I have:
# on server
Meteor.methods
getProducts: (limitNo, skipNo) ->
productsCursor = Products.find( {}, {
limit: limitNo,
skip: skipNo
})
productsCursor.fetch()
this Meteor method returns me next set of products from Products collection.
Of course view template displays ProductsDisplayed collection content:
Template.products.products = ->
ProductsDisplayed.find {}
So, what do you think?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With