【发布时间】:2012-12-19 17:26:47
【问题描述】:
我构建了应用程序,从 mongodb 加载初始数据集需要时间,我想显示加载 gif,直到数据加载完成。你能帮我做这件事吗?
【问题讨论】:
标签: meteor
我构建了应用程序,从 mongodb 加载初始数据集需要时间,我想显示加载 gif,直到数据加载完成。你能帮我做这件事吗?
【问题讨论】:
标签: meteor
在订阅完成时调用的Meteor.subscribe()函数的onReady()回调中使用Session。
Meteor.subscribe('subscribe_to_this', function onReady(){
// set a session key to true to indicate that the subscription is completed.
Session.set('subscription_completed', true);
});
然后将此会话值从您的模板助手返回为:
Template.myTemplate.isSubscriptionComplete = function(){
return Session.get('subscription_completed');
}
现在在您的 html 中,如果数据未加载,则很容易显示加载器;如果数据已完成加载,则可以轻松呈现模板。
<template name="myTemplate">
{{#if isSubscriptionComplete }}
<!-- Data loading is done, so render your template here -->
{{> yourFinalTemplate}}
{{else}}
<!-- Data loading still remaining, so display loader here -->
<img src="images/load.gif">
{{/if}}
</template>
【讨论】:
if Session.get("subscription_completed") is true $(".main-container").addClass("loading") else $(".main-container").addClass("loading")
这可以通过 Session 变量来完成。这只是一个让您入门的示例:
在您的客户端代码中:
var yourData;
Meteor.startup(function() {
Session.set("dataLoaded", false);
});
...
// Load your initial data
yourData = SampleCollection.find(query).fetch();
Session.set("dataLoaded", true);
一个示例模板:
<template name="main">
{{#if dataLoaded}}
<h1>Welcome to my application!</h1>
Your data:
{{#each yourData}}
...
{{/each}}
{{else}
<div>Loading data...</div>
{{/if}}
</template>
模板助手:
Template.main.dataLoaded = function() {
return Session.get("dataLoaded")
}
Template.main.data = function() {
return yourData;
}
【讨论】: