は、クライアント側では、この
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
});
});
ようpageNumber
と呼ばれるパラメータを受け入れるようにパブリケーションを変更し
、私は多くの角度-流星では動作しませんでした。 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.
});
});
のようなものを使用してクライアントに送信フィールドを制限する場合がありますそして最後に、あなたは、改ページのリンクを表示するために、クライアント側でポストコレクション内のレコードの合計数が必要になります。あなたはそれが別の出版物を使用し、公式ドキュメント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..
}
これが役に立てば幸いで述べたようにobserveChanges
概念を使用して行うことができます。
cDate.timestampフィールドにインデックスを追加しようとしましたか?この記事で説明したようにインデックスを追加してください、http://stackoverflow.com/questions/9730136/how-to-create-a-nested-index-in-mongodb 'db.Posts.ensureIndex({" cDate.timestamp ":1 }) ' – Kishor
これを試してみましょう – StormTrooper
インデックスを追加しようとしましたか?動作しません。同じ問題。ブラウザが止まっています – StormTrooper