Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor:observeChanges在模板完全加载后进行更改_Meteor - Fatal编程技术网

Meteor:observeChanges在模板完全加载后进行更改

Meteor:observeChanges在模板完全加载后进行更改,meteor,Meteor,我正在试验meteor,我面临一些代码结构问题。 我想要什么: 我使用观察者来跟踪集合中添加的新文档,但我希望只有在模板完全呈现后才能收到“通知” 在我的router.js文件中,我有: HospitalListController = RouteController.extend({ action: function() { Meteor.subscribe('hospitals'); this.render('listHospitals'); } }); 我的客户端

我正在试验meteor,我面临一些代码结构问题。 我想要什么: 我使用观察者来跟踪集合中添加的新文档,但我希望只有在模板完全呈现后才能收到“通知”

在我的router.js文件中,我有:

HospitalListController = RouteController.extend({
  action: function() {
    Meteor.subscribe('hospitals');
    this.render('listHospitals');
  }
});
我的客户端listHospital.js文件是

Template.listHospitals.onRendered(function(){
  var first = true;
  hospitalsCursor = Hospitals.find();
  var totals = hospitalsCursor.count();
  var loaded = 0;

  HospitalsHandle = hospitalsCursor.observeChanges({
    added : function(doc){
      if( loaded != totals ){
        loaded++;
      }else{
        console.log("added "+doc);    
      }
    }
  });
});

是否有更好的方法,也许是“流星方法”来实现这一点?

您必须添加一个标志,以便在初始化期间忽略observeChange回调(找到此解决方案)


这应该行得通。

我的实际工作代码:

Template.queueView.onRendered(function(){
  var initialLoaded = false;

  Queues.find().observeChanges({
    added : function(id, doc){
      if( initialLoaded ){
        console.log("added "+id);  
        highlight();
        beep();

      }   
    },
    removed : function(id, doc){
      console.log("removed "+id);
      highlight();
      beep();
    },
    changed : function(){
      console.log('modified!');
      highlight();
      beep();
    }
  });


  Meteor.subscribe('queues', function(){
    initialLoaded = true;
  });

});

好的,那么您想在将集合中的初始文档发送到客户端之后处理“添加”吗?也许可以尝试使用Iron Router的waitOn():,然后我认为您的其余逻辑应该可以工作。刚刚尝试过,但添加的回调在模板加载过程中被触发!不工作,因为observeChanges是异步的。
Template.queueView.onRendered(function(){
  var initialLoaded = false;

  Queues.find().observeChanges({
    added : function(id, doc){
      if( initialLoaded ){
        console.log("added "+id);  
        highlight();
        beep();

      }   
    },
    removed : function(id, doc){
      console.log("removed "+id);
      highlight();
      beep();
    },
    changed : function(){
      console.log('modified!');
      highlight();
      beep();
    }
  });


  Meteor.subscribe('queues', function(){
    initialLoaded = true;
  });

});