0

Let's say I want to display a sorted list of family:

# publications
Meteor.publish "families", ->
  Families.find({}, {sort:{name:1}})

# routes
@route 'families',
    path: '/families'
    waitOn: ->
      return Meteor.subscribe('families')
    data: ->
      families: Families.find() 
      # or
      families: Families.find({}, {sort:{name:1}})

I tested and it seems both are working fine. What's the best practices here?

AZ.
  • 7,333
  • 6
  • 44
  • 62

2 Answers2

3

So as I explained in the answer to this question, sorting in the publish function has no affect on the order of documents on the client. So you should see the same result if you do:

Meteor.publish "families", ->
  Families.find()

As for your template data, that should specify the sort (your second choice). It may be that both options yield the same result because of their insertion order but that's coincidental.

Community
  • 1
  • 1
David Weldon
  • 63,632
  • 11
  • 148
  • 146
1

Not being expert, what I see here looks for me like the alternative between sorting on server and sorting at client. Well, the work has to be done anyway for each request - letting it do at the client will relieve the server, which typically has enough work anyway...

So do the latter.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • Usually the right answer is to push work to client as you say. In this case you have to because DDP does not guarantee order so sorting on server would be a waste. – alanning Apr 13 '14 at 13:15