Here is the deal - I have records in a mongo collection called “pages”. Each “page” has an “order” field, which is basically an integer that is used for sorting of the records later. I have a drag & drop UI for pages reordering. On every drop, I send the new pages order as array of objects like this:
[{_id:'2', order:0},{_id:'5', order:1},{_id:'1', order:2}]
On the server I update the pages collection like this:
newOrder.forEach(function(page){
Pages.update(page._id, { $set: {order: page.order} });
});
Then in the client I subscribe for the Pages collection and I have a getMeteorData method because I use React:
getMeteorData () {
return {pages: Pages.find({}, {sort: {order: 1}}).fetch()}
},
This works… kinda. The problem is that the client that sends the new order doesn’t update the UI properly. It gets partial server results and partial minimongo client results at the same time, because updating happens too fast (I work on localhost and in real life it won’t be so fast, but still it is an issue).
Anyways, calling Pages.update
for every single page is far from optimal, I know. And this brings the problem with partial updates too. Any idea how could I make it better? Some way to prevent data server push until the forEach loop finishes and then push the changes at once? Or to remove the client-side minimongo storage for that particular collection and make it update only after server response? Or a better reordering storing approach? Any idea is more than welcome!