ページネーションの例では、表示する必要がある結果の数に応じて、別の番号がルートに渡されます。サブスクリプションが既にデータセットを制限している場合、データを制限する必要がありますか?
// lib/router.js
Router.route('/:postsLimit?', {
name: 'postsList',
waitOn: function() {
var limit = parseInt(this.params.postsLimit) || 5;
return Meteor.subscribe('posts', {sort: {submitted: -1}, limit: limit});
},
data: function() {
var limit = parseInt(this.params.postsLimit) || 5;
return {
posts: Posts.find({}, {sort: {submitted: -1}, limit: limit})
};
}
});
データ属性にPosts.find()引数を指定する必要があるかどうかは疑問です。これは、waitOnのサブスクリプションがすでにサーバーから戻ってくるデータセットをすでに制限しているため、ちょっと冗長なようです。私は、データがテンプレートのさまざまな部分にデータコンテキストを提供するために使用されることを知っています。しかし、ここでデータPosts.find()と同じ引数を指定するだけでは冗長なようです。引数なしでPosts.find()を試してみたところ、うまくいきました。
また、ルータのすべてのデータにアクセスする方法は何ですか?私は個々のルートの外でPostsコレクションにアクセスしています。私はPostsコレクションのすべてのデータにアクセスできると思っていました。しかし、それは数で返さ0
// lib/router.js
var totalPostsCount = Posts.find().count()
console.log('total count?')
console.log(totalPostsCount) // output 0
PostsListController = RouteController.extend({
template: 'postsList',
increment: 5,
postsLimit: function() {
return parseInt(this.params.postsLimit) || this.increment;
},
findOptions: function() {
return {sort: {submitted: -1}, limit: this.postsLimit()};
},
subscriptions: function() {
this.postsSub = Meteor.subscribe('posts', this.findOptions());
},
posts: function() {
// console.log("is find limited to subscriptions too??");
// console.log(Posts.find().count())
return Posts.find({}, this.findOptions());
},
data: function() {
// console.log('is data limited to this?')
// console.log(Posts.find().count())
var hasMore = this.posts().count() === this.postsLimit();
var adjustOrNot = this.posts()
if (!hasMore){
var nextPath = this.route.path({postsLimit: this.posts().count()});
}else{
var nextPath = this.route.path({postsLimit: this.postsLimit() + this.increment});
}
return {
posts: this.posts(),
ready:this.postsSub.ready(),
nextPath: hasMore ? nextPath : null
};
}
});
//...
Router.route('/:postsLimit?', {
name: 'postsList'
});
編集1:コンソールで「まだ準備ができていない」
// server
Meteor.publish('allPosts',function(){
return Posts.find({});
})
// lib/router.js
if (Meteor.isClient){
var handle = Meteor.subscribe('allPosts');
if (handle.ready()) {
// This logs the actual count of posts
console.log(Posts.find().count());
} else {
console.log('not ready yet');
}
}
これだけ出力し、それも変更していない:すべての記事を取得するためのコードページが読み込まれたら
ありがとうございました。
編集2:可能な解決策
Iは、()Tracker.autorunとして計算内部レディ反応ソース()をラップしようとした今では働きました。
if (Meteor.isClient){
var handle = Meteor.subscribe('allPosts');
Tracker.autorun(function(){
var status = handle.ready();
if (status){
console.log(Posts.find().count())
}else{
console.log('not ready yet')
}
})
}
は、最初の部分はあまり意味を成していました。しかし、2番目の部分では、ready()を使用しようとしましたが、何も表示されません。新しいコードを質問に追加しました。 – whales
反応内のソースを計算の中にラップしていないからですか?私はTracker.autorunの中にそれをラップしようとしました。 – whales
@whalesはい、それはひどく問題でした。メテオ反応性は、 'iron:router'ルート定義のテンプレートヘルパーである' Tracker.autorun'のような反応的なコンテキスト( 'subscriptionHandle.ready()'など)が反応コンテキストで呼び出されたときに機能します。これらの機能の外では、Meteorは反応的なデータソースとしてのコードを再実行することはできません。 – Waiski