Reputation: 2481
I've been making a simple blog site using Meteor.js. In the index, the page shows recent posts and the list can be rearranged by their tags and category. It also (for sure) has separate pages for each post.
When the index is refreshed, the list is ordered well, sorted by publishedAt
. When I try to arrange posts by tags or category, or even when I go to see an individual post, it's good. But when I go back to the index, the last fetched posts (or a post in the latter case) are always on the top of the list, not properly sorted by publishedAt
.
For example, let's say we have the list of 4-3-2-1. By sorting them, I have 3-2. When I go back to the index, now the list goes like 3-2-4-1.
When I console.log()
from client.js
, those last fetched posts are logged first, and then the other posts including the logged ones are logged. How can I fetch posts without being affected by previous fetched data?
Posts = new Mongo.Collection("posts");
Router.route('/', {
name: 'index',
template: 'index',
layoutTemplate: 'ApplicationLayout',
waitOn: function() {
return Meteor.subscribe('recentPosts');
},
data: function() {
return {
posts: Posts.find()
};
}
});
Router.route('/category/:_category', {
name: 'category',
template: 'category',
layoutTemplate: 'ApplicationLayout',
waitOn: function() {
return Meteor.subscribe('categorizedPosts', this.params._category);
},
data: function() {
return {
posts: Posts.find(),
category: this.params._category
};
}
});
Posts = new Mongo.Collection("posts");
Meteor.publish('recentPosts', function() {
return Posts.find({}, {
sort: {publishedAt: -1},
fields: postListFields
});
});
Meteor.publish('categorizedPosts', function(category) {
return Posts.find({
$or: [{category: {$regex: category}}]}, {
sort: {publishedAt: -1},
fields: postListFields
});
});
Upvotes: 0
Views: 187
Reputation: 15442
The order of the locally stored data has no relation to the order of the documents that the server gives you (as you've discovered). Consider also the case where multiple publications with different sort orders are sent to one local collection - how could that be handled?
In your client code's data context you need to specify a sort order to the find() method. This will sort the locally stored posts using MiniMongo. It will not cause a new server request, only the subscribe method does that.
You may be wondering what the point is sorting on the server? As an example, if you're limiting the number of posts that you're sending to the client then it's very important that the server sorts them first so that it sends the right ones (e.g. the first 10 of posts sorted by date) over the DDP link.
A result of this is that you very often need to replicate the sort order for the find() method on both the server and client. You may wish to try to user some common code or variable to ensure that they're in sync.
Upvotes: 1
Reputation: 70
On your "Posts" collection try with a method submitted: new Date()
, then sort with it in your helper : return Posts.find({}, {sort: {submitted: -1}});
Upvotes: 1