简体   繁体   English

如何在流星中加载页面后调用函数

[英]How to call function after page load in meteor

Iam using cursor.observechanges to see whether there is any new record inserted in mongo and giving a notification as new recored inserted and it is working good.But the issue here is when my app is loaded first time,Iam getting those notification alerts since it observed those changes.Same way when I click on next page. 我使用cursor.observechanges来查看mongo中是否插入了新记录,并在新的recored插入时发出通知并且工作正常。但这里的问题是我的应用程序第一次加载时,我收到了那些通知警报,因为它观察了那些变化。点击下一页的方式。

cursor.observeChanges({
  added: function(id, object) {
    Notification.error('NEW Record',object);
    var audio = new Audio('audio.mp3');
    audio.play();
  }
});

So I need to call this fuction only after the page gets loaded.I tried using 所以我需要在页面加载后调用此函数。我尝试使用

Template.foo.onRendered(function () {
  cursor.observeChanges({
    added: function(id, object) {
      Notification.error('NEW Record',object);
      var audio = new Audio('audio.mp3');
      audio.play();
    }
  });
});

But it didnt worked in my case.Is there any other way we can do this.Or else if possible how can we set a time of 5 secs and call the above fuction after that time interval? 但它在我的情况下没有用。我还有其他方法可以做到这一点。或者如果可能的话,我们如何设置5秒的时间并在该时间间隔后调用上述功能? Any help would be appreciated.thanks! 任何帮助将不胜感激。谢谢!

According to docs Template#onRendered : 根据文档 Template#onRendered

Register a function to be called when an instance of this template is inserted into the DOM. 注册将此模板的实例插入DOM时要调用的函数。

Try to wrap your code in Meteor.defer : 尝试将您的代码包装在Meteor.defer

Template.foo.onRendered(function() {
  Meteor.defer(function() {
    cursor.observeChanges({
      added: function(id, object) {
        Notification.error('NEW Record',object);
        var audio = new Audio('audio.mp3');
        audio.play();
      }
    });
  })
});

If it will not help, try to wrap it in Meteor.setTimeout : 如果它没有帮助,请尝试将其包装在Meteor.setTimeout

Template.foo.onRendered(function() {
  Meteor.setTimeout(function() {
    cursor.observeChanges({
      added: function(id, object) {
        Notification.error('NEW Record',object);
        var audio = new Audio('audio.mp3');
        audio.play();
      }
    });
  }, 5000)
});

the behaviour is correct because, to the template, those records are indeed added. 行为是正确的,因为对于模板,确实添加了那些记录。 browse away and browse back, which re-creates the template, and now all those same records are once again new to the template. 浏览并浏览,重新创建模板,现在所有这些相同的记录再次成为模板的新记录。

you need to separate the concept of records being new to the template and records being new to the app. 您需要将新记录的概念与模板分开,并将记录作为应用程序的新记录。

i might solve that with a timestamp. 我可以用时间戳解决这个问题。 eg 例如

  1. write a Meteor method that provides the current time of the server 编写一个提供服务器当前时间的Meteor方法
  2. in the template's onCreated(), call that method and save off the result 在模板的onCreated()中,调用该方法并保存结果
  3. in the added: handler, compare the creation timestamp of the added item to that of the saved time 在添加的:处理程序中,将添加项的创建时间戳与保存时间的创建时间戳进行比较
  4. if the added item is older, do nothing 如果添加的项目较旧,则不执行任何操作
  5. if the added item is newer, take your action 如果添加的项目较新,请采取您的行动

this means your collection will need a createdAt field. 这意味着您的收藏将需要一个createdAt字段。

this solution should work both for reloading the app, browsing to/from the template, and a long-running template. 此解决方案既可用于重新加载应用程序,也可用于浏览模板或从模板进行浏览以及长时间运行的模板。

for best effect, don't start your subscription until the Meteor method call has returned with the server time. 为了获得最佳效果,请在Meteor方法调用返回服务器时间之前不要开始订阅。 all this should happen in onCreated(). 所有这些都应该在onCreated()中发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM