【发布时间】:2014-04-14 07:48:02
【问题描述】:
我在 gnf.meteor.com 上有一个 Meteor 应用程序,用于在我家的商店运行的项目中进行捐赠活动。该应用程序本身与这个问题不太相关,但它提供了简单的信用卡结账功能,可以连接到贝宝,并为我们跟踪生成的交易日志和余额。
我的应用程序中与此问题相关的页面是https://gnf.meteor.com/log。此页面记录了网站上的最新捐赠、捐赠者、类型、金额和接受者。
当您第一次在 /log 加载或重新加载页面时,Meteor 需要 7-10 秒才能呈现正确的数据。在此间隔期间,它首先显示一个空列表,然后在几秒钟后它会显示一些较旧的记录(不是最新数据),最后它会使用正确的记录重新渲染。我猜可能集合被渲染,然后排序,然后重新渲染,但我不知道这是否真的是一个排序问题,它可能只是先加载旧记录,在加载完成之前渲染。
为了说明我的意思,加载页面并观察在撰写本文时最新的捐赠(列表顶部)应该是“Maura M. $70 -> Suzie Murphy”。 (这个应用程序还没有被大量使用,所以它会在一段时间内保持最新的交易。)
如果可能,我想避免这种情况,并且我还计划根据this question 的提问者的要求引入加载消息。
我也讨厌一开始加载需要很长时间,尽管我的交易集合中只有大约 300 条记录。也许我可以以某种更有效的方式实现相同的查询?
这是我的应用程序中的相关代码:
在我的 Javascript 中:
Data = {
funds : new Meteor.Collection('funds'),
transactions : new Meteor.Collection('transactions')
};
// ...
Template.logPage.latestDonations = function() {
return Data.transactions.find({
$or : [{ type : 'donation' }, { type : 'deposit' }]
}, {
sort : { timestamp : -1 },
limit : 50
});
};
Template.logPage.typeDonation = function() {
return this.type == "donation";
};
Template.logPage.typeDeposit = function() {
return this.type == "deposit";
};
在我的 HTML 中:
<template name="logPage">
<h1 class="underline">Latest 50 Donations <small>(updates in real time)</small> </h1>
{{#each latestDonations}}
<h2>
{{donorName}}
{{#if typeDonation}}
<i class="fa fa-credit-card"></i>
{{/if}}
{{#if typeDeposit}}
<i class="fa fa-money"></i>
{{/if}}
${{amount}} <i class="fa fa-arrow-right"></i>
<a href="/fund/{{fund_id}}">{{recipientName}}</a>
<small><abbr class="timeago" title="{{donationTimestamp}}">
{{donationTimeString}}</abbr>
</small>
</h2>
{{/each}}
</template>
提前感谢任何人可能拥有的任何见解。 Meteor 很棒,但这些小烦恼让我很沮丧。
【问题讨论】:
标签: javascript performance mongodb templates meteor