更改您的出版物,接受一個名爲pageNumber
參數這樣
Meteor.publish('posts', function (pageNumber) {
var numberOfRecordsPerPage = 10;
var skipRecords = numberOfRecordsPerPage * (pageNumber - 1);
return Post.find({
"user_id": user_id
}, {
sort: { 'cDate.timestamp': -1 }
skip: skipRecords,
limit: numberOfRecordsPerPage
});
});
在客戶端,我沒有角流星一樣工作。您可以使用this.pageNumber
或$scope.pageNumber
在當前範圍下創建pageNumber
屬性。只要您的分頁頁面被點擊,更新這個pageNumber
變量。只要此變量發生更改,請使用當前頁碼進行訂閱。
如果是使用標準模板火焰,我會用一個autorun
這樣的反應VAR或會話變種做到這一點。 在模板的html:
<template name="postsTemplate">
<ul>
<!-- you would want to do this list based on total number of records -->
<li class="pagination" data-value="1">1</li>
<li class="pagination" data-value="2">2</li>
<li class="pagination" data-value="3">3</li>
</ul>
</template>
在模板JS:
Template.postsTemplate.created = function() {
var template = this;
Session.setDefault('paginationPage', 1);
template.autorun(function() {
var pageNumber = Session.get('paginationPage');
Meteor.subscribe('posts', pageNumber);
});
}
Template.postsTemplate.events(function() {
'click .pagination': function (ev, template) {
var target = $(ev.target);
var pageNumber = target.attr('data-value');
Session.set('paginationPage', pageNumber);
}
});
這樣,你會在客戶端上的任一時間點最多的10個記錄都有,所以它不會崩潰的瀏覽器。您可能還希望限制你使用這樣的事情
Meteor.publish('posts', function (pageNumber) {
var numberOfRecordsPerPage = 10;
var skipRecords = numberOfRecordsPerPage * (pageNumber - 1);
return Post.find({
"user_id": user_id
}, {
sort: { 'cDate.timestamp': -1 }
skip: skipRecords,
limit: numberOfRecordsPerPage,
fields: {'message': 1, 'createdBy': 1, 'createdDate': 1 } //The properties inside each document of the posts collection.
});
});
發送到客戶端領域的最後,你將需要記錄的總數在帖子中收集的客戶端,以顯示分頁鏈接。您可以使用不同的發佈和使用observeChanges
概念作爲正式文件here
// server: publish the current size of a collection
Meteor.publish("posts-count", function() {
var self = this;
var count = 0;
var initializing = true;
// observeChanges only returns after the initial `added` callbacks
// have run. Until then, we don't want to send a lot of
// `self.changed()` messages - hence tracking the
// `initializing` state.
var handle = Posts.find({}).observeChanges({
added: function (id) {
count++;
if (!initializing)
self.changed("postsCount", 1, {count: count});
},
removed: function (id) {
count--;
self.changed("postsCount", 1, {count: count});
}
// don't care about changed
});
// Instead, we'll send one `self.added()` message right after
// observeChanges has returned, and mark the subscription as
// ready.
initializing = false;
self.added("postsCount", 1, {count: count});
self.ready();
// Stop observing the cursor when client unsubs.
// Stopping a subscription automatically takes
// care of sending the client any removed messages.
self.onStop(function() {
handle.stop();
});
});
// client: declare collection to hold count object
PostsCount = new Mongo.Collection("postsCount");
// to get the total number of records and total number of pages
var doc = PostsCount.findOne(); //since we only publish one record with "d == 1", we don't need use query selectors
var count = 0, totalPages = 0;
if (doc) {
count = doc.count;
totalPages = Math.ceil(count/10); //since page number cannot be floating point numbers..
}
希望這有助於在提到做。
你嘗試在cDate.timestamp場添加索引。添加索引在這篇文章中提到,http://stackoverflow.com/questions/9730136/how-to-create-a-nested-index-in-mongodb'db.Posts.ensureIndex({「cDate.timestamp」:1 })' – Kishor
好吧,讓我試試這個 – StormTrooper
嘗試添加索引。不起作用。同樣的問題。瀏覽器卡住 – StormTrooper