meteor; not sorting on client immediately after publication

妖精的绣舞 提交于 2019-12-23 23:34:53

问题


I am trying to make a very simple application where one can publish posts. I want each entry on the post page to be sorted by { createdAt: -1 }. When I first visit the page, this works. However, when I make a new post, it appends to the bottom of the page.

(notice latest post is at bottom). When I reload the page, it does seem to work and sort correctly, but immediately after submitting, it goes to the bottom.

My serverside code is as follows (/server/publications.js):

Meteor.publish('posts', function(limit) {
  limit = limit || Meteor.settings.public.pageLimit;
  check(limit, Number);
  var options = {
    sort: { createdAt: -1 },
    limit: limit
  };
  return Posts.find({}, options);
});

On my client, I have this (/client/subscriptions.js):

Meteor.subscribe('posts');

How can I make the posts sort correctly immediately after submitting?


回答1:


Lets make that subscribe reactive.

Tracker.autorun(function(){
  Meteor.subscribe('posts');
}

Why don't make a simple Publish-Subscribe

and use this helper? on the client side.

Template.postsExample.helpers({
  posts: function() {
    return Posts.find({}, {sort: {submitted: -1}});
  }
});



回答2:


I think the solution is fairly obvious: you should sort on the client side, not the server side. I'm guessing you have a helper function that contains a query for the posts? Add the sort there.

Underlying explanation: Meteor tries to push the smallest amount of information to the client upon insert/update. I'm guessing it is not fully re-running the subscription server side, which is what you are expecting, but it's only sending the last added element, which gets appended to the client-side collection.

(Note: to be 100% correct, you'll need the sorting on both sides as you are limiting the size of the result set as well and you only want the most recent to be pushed.)




回答3:


Can you try this and let me know?

Meteor.publish('posts', function(limit) {
  limit = limit || Meteor.settings.public.pageLimit;
  check(limit, Number);
  var options = {
    sort: {submitted: -1},
    limit: limit
  };
  return Posts.find({}, options);
});



回答4:


Since you're using sorting and limiting to retrieve the most recent posts, you need to continue publishing the function as you are now. The sort in your publish function is what allows the server to send up to the client only the most recent [limit] posts.

When a new post is added, the server doesn't resort all of the documents, it just sends up the new addition, which is added to Minimongo on the client. It's up to the client to sort the records that it draws from Minimongo.

If your template is like this:

<template name="listOfPosts">
    {{#each posts}}
        <div>{{content}}</div>
        <div>Submitted by: {{author}}</div>
        <div>{{submitted}}</div>
    {{/each}}
</template>

Then your helper that feeds the template should have a sort specifier:

Template.listOfPosts.helpers({
    posts: function() {
        return Posts.find({}, {sort: {submitted: -1}});
    }
});


来源:https://stackoverflow.com/questions/28813567/meteor-not-sorting-on-client-immediately-after-publication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!