2016-06-13 43 views
0

我有一個集合,像這樣的結構:流星迴限定值從子陣列

{ 
    _id: xKdshsdhs7h8 
    files: [ 
     { 
     file_name : "1.txt", 
     file_path : "/home/user1/" 
     }, 
     { 
     file_name : "2.txt", 
     file_path : "/home/user2/" 
     } 
    ] 
} 

而且在服務器即時試圖返回遊標,我可以用它來顯示在客戶端這一切文件。另外我想在頁面上輸出數量有限的文件,並讓用戶點擊「加載更多」以顯示更多內容。 所以我不能對究竟是如何做到這一點搞清楚,目前我有:

Meteor.publish("attachments_list_limited", function (count,id) { 


     var test = AttachmentsList.find({_id : id},{limit: count}, {sort: {"files.fileName": -1}}); 

     return test; 

    }); 

所以理論上我會得到客戶的單個對象,我可以這樣

{{#each attachmentsList.files}} 

         <li class="list-group-item col-xs-3 borderless"> 

          <span data-id={{_id}} class="pull-right"> 

           <button class="btn btn-xs btn-delete-attachment" data-toggle="tooltip" data-placement="top" title="Delete"> 
            <span class="glyphicon glyphicon-remove"></span> 
           </button> 
          </span> 

          <div class="panel panel-default"> 
           <div class="panel-body wrapped attachment">{{fileName}}</div> 
          </div> 

         </li> 
    {{/each}} 
輸出

但是我怎樣才能返回一個光標到極限的數組元素?

回答

0

改爲將發佈功能限制爲countid,而不是將選擇器和選項。

因此,它看起來像這樣

Meteor.publish("attachments_list_limited", function (selector, options) { 
    var test = AttachmentsList.find(selector, options); 
    return test; 
}); 

然後在客戶端,您可以設置使用一個會話變量或類似的東西的限制。點擊按鈕時增加它。

Session.setDefault('count', 5); 

helper: function() { 
    var selector = ... 
    var options = ... 
    return Attachments.find(selector, options); 
} 

'click .load': function() { 
    Session.set('count', Session.get('count') + 5); 
} 
+0

讓客戶端使用任意選擇器似乎有風險。 –

0

你把它錯在這裏:

AttachmentsList.find({_id : id},{limit: count}, {sort: {"files.fileName": -1}}); 

它應該是這樣的:

AttachmentsList.find({_id : id}, {sort: {"files.fileName": -1}, limit: count}); 

讓我知道,如果它不工作,我給你一個完整的例。