Meteor Subscribe doesn't update sort order of collection

前端 未结 2 1186
无人共我
无人共我 2020-11-30 06:33
// Snippet from Template
{{#each elements}} {{> post-element this}} {{/each}}
// Snippet from Client M
相关标签:
2条回答
  • 2020-11-30 07:09

    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.

    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.

    0 讨论(0)
  • 2020-11-30 07:18

    You didn't posted your template helper code.

    When you do return Posts.find() from the helper function, the query should also contain the sort arguments, like as below:

    Template.myTemplate.elements = function(){
       Meteor.subscribe('thePosts');
       return Posts.find({},  {sort:{createdAt:-1}, reactive:true});
    }
    
    0 讨论(0)
提交回复
热议问题