流星排序find()怎么样?

时间:2016-01-12 14:36:07

标签: meteor meteor-blaze

我想在meteor中创建一个ToDo列表webapp,用户可以在其中定义自己的目标并对其进行排序,因此目标越高,优先级就越大。

我有两个收藏: 流星:PRIMARY> db.goals.find()

{ "_id" : "ZDCwfD4pWfD7sZ9DB", "gtext" : "Money", "createdAt" : ISODate("2015-12-26T13:15:59.563Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "order" : 0 }
{ "_id" : "B9oKRGssNWhjz57dP", "gtext" : "Procrastination", "createdAt" : ISODate("2015-12-26T13:16:06.272Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "order" : 3 }
{ "_id" : "famqG5vby9wBo37Ns", "gtext" : "Housework", "createdAt" : ISODate("2016-01-01T15:01:02.256Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "order" : 2 }
{ "_id" : "QKWJjTEwZQs5ErrPR", "gtext" : "Getting fit", "createdAt" : ISODate("2016-01-01T16:10:38.302Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "order" : 1 }
{ "_id" : "ByNyg9uRBH35XXgdi", "gtext" : "test1", "createdAt" : ISODate("2016-01-06T13:40:15.224Z"), "owner" : "JWdrC3bFgmHXHeDFf", "order" : 0 }

流星:PRIMARY> db.tasks.find()

{ "_id" : "gNuyxHdyA6HN8JFsd", "text" : "clean up", "createdAt" : ISODate("2015-12-27T11:12:46.186Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "notes" : "", "priority" : "famqG5vby9wBo37Ns", "postponedate" : "", "checked" : false,  "relatedgoal": {    "order": 2,    "_id": "famqG5vby9wBo37Ns" }}
{ "_id" : "6dEkaETWAkhsELP8r", "text" : "Sell something", "createdAt" : ISODate("2015-12-27T11:12:48.669Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "notes" : "", "priority" : "ZDCwfD4pWfD7sZ9DB", "postponedate" : "",   "relatedgoal": {    "order": 0,    "_id": "ZDCwfD4pWfD7sZ9DB"} }
{ "_id" : "nCKz9oCkiExz4ekfK", "text" : "Win the lottery", "createdAt" : ISODate("2015-12-27T11:12:51.294Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "notes" : "", "priority" : "ZDCwfD4pWfD7sZ9DB", "postponedate" : "",  "relatedgoal": {    "order": 0,    "_id": "ZDCwfD4pWfD7sZ9DB"  } }
{ "_id" : "YvWxaxWM9qvGzkpEK", "text" : "read a magazine", "createdAt" : ISODate("2015-12-27T11:12:41.526Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "notes" : "", "priority" : "B9oKRGssNWhjz57dP", "postponedate" : ""   "relatedgoal": {    "order": 3,    "_id": "B9oKRGssNWhjz57dP"  }}
{ "_id" : "s53vn9tf8C5tyy27q", "text" : "task1", "createdAt" : ISODate("2016-01-06T13:39:36.673Z"), "owner" : "JWdrC3bFgmHXHeDFf", "notes" : "", "priority" : "ByNyg9uRBH35XXgdi", "postponedate" : "" }
{ "_id" : "bEWosRJK4ekAvSuJ6", "text" : "do some situps", "createdAt" : ISODate("2016-01-08T15:47:41.047Z"), "owner" : "qDqGDaXjaHXNhX95u", "username" : "prsz", "notes" : "", "priority" : "QKWJjTEwZQs5ErrPR",    "relatedgoal": {    "order": 1,    "_id": "QKWJjTEwZQs5ErrPR"  }}

我的最终目标是能够按相关目标的订单字段对任务进行排序。 我正在使用perak / meteor-joins将任务集合与相关目标的oder字段连接起来。 现在我的问题是我无法通过oder字段对我的find()进行排序。

client.js:

Meteor.subscribe('Tasks');
Meteor.subscribe('Goals');

Template.body.helpers({
    priorityTasks: function() {
        return Tasks.find(
            {postponedate: {$exists: false }},
            {sort: {'relatedgoal.order': -1}}
        )
    }
});

我甚至尝试过,但这并没有帮助:

priorityTasks: function() {
    return Tasks.find(
        {postponedate: {$exists: false }},
        {sort: {relatedgoal.order: 1}}
    )  
}),

有没有办法按订单字段对任务进行排序? 我正在考虑在find方法中进行转换,但这不是反应性的。我需要一个反应性的解决方案。

Server.js:

Tasks = new Mongo.Collection("tasks");                            
Goals = new Mongo.Collection("goals");
Tasks.join(Goals, "priority", "relatedgoal", ["order"]);

Meteor.publish('Tasks',function() {
    return Tasks.find({ owner: this.userId}); 
});

Meteor.publish('Goals',function() {
    return Goals.find({ owner: this.userId}); 
});
Sortable.collections = ['goals'];

todo.html

<div class="container-fluid col-md-6">   
    <ul>
    {{#each priorityTasks}}       
        {{> task}}
    {{/each}}
    </ul>
</div> 

<template name="task">
    <li>
        <span class="text">
            <a href="#modal-5" data-toggle="modal">
                <strong>{{text}} - {{relatedgoal.order}}</strong>
            </a>
            <br>
            {{postponedate}}
        </span>
    </li>    
</template>

1 个答案:

答案 0 :(得分:0)

priorityTasks: function() {
  return Tasks.find(
      {
      postponedate: {$exists: false }
      }, {
         sort: {'relatedgoal.order': 1}})  
   },

应该有效