流星订阅不会更新集合的排序顺序 [英] Meteor Subscribe doesn't update sort order of collection
问题描述
// Snippet from Template
<div class="post-container">
{{#each elements}}
{{> post-element this}}
{{/each}}
</div>
// Snippet from Client
Meteor.subscribe('thePosts');
// Snippet from Server
Meteor.publish('thePosts', function(){
return Posts.find({}, {sort:{createdAt:-1}, reactive:true});
});
当我这样做时...
Posts.insert({body:postBody, createdAt: new Date()});
帖子文档被添加并出现在列表的末尾,与我的发布功能中指定的降序相反.对我在做什么错有任何想法吗?
The post document gets added and appears at the end of my list, as opposed to descending order as specified in my publish function. Any idea about what I am doing wrong?
谢谢!
推荐答案
publish函数确定哪些记录应同步到任何订阅客户端的mini-mongo数据库.因此,使用发布功能对数据进行排序实际上对客户端没有任何影响,因为客户端数据库可能会以其他方式存储它们.
The publish function determines which records should be synced to the mini-mongo database of any subscribing clients. So sorting the data in the publish function actually has no effect on the client, as the client-side database will likely store them in some other way.
当然,您可能想在发布者的find
中使用sort,以将记录数限制为最近的N条-但这只是决定同步哪些记录而不是如何同步记录的一种方式.由客户存储/使用.
Of course you may want to use sort in a publisher's find
in order to limit the number of records to the N most recent - but again this is just a way of deciding which records get synced and not how they are to be stored/used by the client.
一旦记录已同步到客户端,则由模板代码决定如何显示结果.例如:
Once the records have been synced to the client, it is up to the template code to determine how the results should be displayed. For example:
Template.myTemplate.elements = function() {
return Posts.find({}, {sort: {createdAt:-1}});
}
另请参阅我在常见错误上的帖子的排序发布"部分.
Also see the "sorted publish" section of my post on common mistakes.
这篇关于流星订阅不会更新集合的排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!