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});
});
แม่แบบ:
<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>
และคอนโซลเอาต์พุตเมื่อโหลดเพจ:
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
ดังนั้นฟังก์ชันตัวช่วยจะเริ่มทำงาน 10 ครั้งในการโหลดหน้าเว็บ (จำนวนครั้งจะแตกต่างกันไป) ใครช่วยอธิบายสิ่งที่เกิดขึ้นที่นี่ได้ไหม ฉันไม่พบการอ้างอิงถึงสิ่งนี้ ยอมรับในสถานการณ์ที่มีการเรียกตัวช่วยจากหลาย ๆ {{ }} บนเทมเพลต แล้วมีวิธีหยุดมันบ้างไหม? ในที่สุด ฉันก็ต้องประมวลผลทวีตในคราวเดียวก่อนจึงจะแสดงผลได้