1

I built the application and it is taking time to load the initial set of data from mongodb and I want to show the loadin gif till the data loading is done. Could you please help me in doing this?

4

2 回答 2

8

Session在函数的onReady()回调中使用Meteor.subscribe(),订阅完成时调用。

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>
于 2013-01-05T11:50:32.717 回答
2

这可以通过 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;
}
于 2013-01-05T11:47:25.047 回答