Tweets = new Meteor.Collection('tweets');
if (Meteor.isClient) {
Meteor.subscribe('tweets');
Template.Panel.helpers({
items: function() {
var days_tweets = Tweets.find();
console.log(days_tweets.count());
return days_tweets;
});
}
if (Meteor.isServer) {
Meteor.publish('tweets', function() {
return Tweets.find({}, {limit: 1000});
});
Templat:
<body>
<h1>This is a list of tweets</h1>
{{> Panel}}
</body>
<template name="Panel">
<h2>A list of tweets sorted by size</h2>
{{#each items}}
<p>item</p>
{{/each}}
</template>
Dan keluaran konsol saat halaman dimuat:
Tweet count: 0
Tweet count: 129
Tweet count: 272
Tweet count: 366
Tweet count: 457
Tweet count: 547
Tweet count: 672
Tweet count: 814
Tweet count: 941
Tweet count: 1000
Jadi fungsi pembantu diaktifkan 10 kali saat memuat halaman (jumlahnya bervariasi). Adakah yang bisa menjelaskan apa yang terjadi di sini? Saya tidak dapat menemukan referensi apa pun untuk ini, terima dalam situasi di mana helper dipanggil dari beberapa {{ }} pada template. Juga ada cara untuk menghentikannya? Akhirnya saya perlu memproses tweet sekaligus sebelum dirender.