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
Meteor 流星:Tracker.autorun和dep.changed导致无限循环_Meteor_Meteor Tracker - Fatal编程技术网

Meteor 流星:Tracker.autorun和dep.changed导致无限循环

Meteor 流星:Tracker.autorun和dep.changed导致无限循环,meteor,meteor-tracker,Meteor,Meteor Tracker,我正在使用一个新的Tracker.Dependency来跟踪一些东西,但它会导致下面代码中的自动运行无限期地运行。怎么了?一旦我将getSong和getSongId分离为依赖于dep和dep2,而不仅仅是dep,下面的代码就可以了 SongManager = { dep: new Tracker.Dependency, dep2: new Tracker.Dependency, init: function(songId) { var self = this; thi

我正在使用一个新的Tracker.Dependency来跟踪一些东西,但它会导致下面代码中的自动运行无限期地运行。怎么了?一旦我将getSong和getSongId分离为依赖于dep和dep2,而不仅仅是dep,下面的代码就可以了

SongManager = {
  dep: new Tracker.Dependency,
  dep2: new Tracker.Dependency,
  init: function(songId) {
    var self = this;
    this.setSongId(songId);
    Meteor.subscribe('song', songId);
    Tracker.autorun(function(){
      var songs = self.getSongCursor().fetch();
      if (songs.length > 0) {
        self.song = songs[0];
        self.dep.changed();
      }
    })
  },
  getSongCursor: function(){
    return Songs.find({_id: this.getSongId()});
  },
  getSong: function(){
    this.dep.depend();
    return this.song;
  },
  getSongId: function(){
    this.dep2.depend();
    return this.songId;
  },
  setSongId: function(arg){
    this.songId = arg;
    this.dep2.changed();
  },
};

问题是您正在创建一个循环依赖项。我建议为此使用
ReactiveVar
,而不是使用较低级别的依赖API

meteor add reactive-var
然后你可以这样做:

SongManager={
宋:新的ReactiveVar(),
songId:new ReactiveVar(),
init:函数(songId){
this.songId.set(songId);
this.computation=Tracker.autorun(u.bind(this.update,this));
},
更新:函数(){
var songId=this.songId.get();
Meteor.subscribe('song',songId);
this.song.set(Songs.findOne(songId));
},
停止:函数(){
this.computation.stop();
}
};
SongManager.init(oldSongId);
SongManager.songId.set(newSongId);
//经过足够的时间更新订阅并刷新跟踪器后:
var currentSong=SongManager.song.get();
console.log(currentSong.\u id==newSongId);//真的

我还为您添加了一种停止自动运行计算的方法,这样当不再需要自动运行计算时,它就不会继续在后台运行。请注意,由于订阅是在自动运行中运行的,因此当
songId
更改时,订阅将自动停止并重新启动。更新功能实际上将运行两次,但Meteor知道不发送两个相同的订阅请求。

问题在于您正在创建循环依赖关系。我建议为此使用
ReactiveVar
,而不是使用较低级别的依赖API

meteor add reactive-var
然后你可以这样做:

SongManager={
宋:新的ReactiveVar(),
songId:new ReactiveVar(),
init:函数(songId){
this.songId.set(songId);
this.computation=Tracker.autorun(u.bind(this.update,this));
},
更新:函数(){
var songId=this.songId.get();
Meteor.subscribe('song',songId);
this.song.set(Songs.findOne(songId));
},
停止:函数(){
this.computation.stop();
}
};
SongManager.init(oldSongId);
SongManager.songId.set(newSongId);
//经过足够的时间更新订阅并刷新跟踪器后:
var currentSong=SongManager.song.get();
console.log(currentSong.\u id==newSongId);//真的

我还为您添加了一种停止自动运行计算的方法,这样当不再需要自动运行计算时,它就不会继续在后台运行。请注意,由于订阅是在自动运行中运行的,因此当
songId
更改时,订阅将自动停止并重新启动。更新功能实际上将运行两次,但Meteor知道不发送两个相同的订阅请求。

问题在于您正在创建循环依赖关系。我建议为此使用
ReactiveVar
,而不是使用较低级别的依赖API

meteor add reactive-var
然后你可以这样做:

SongManager={
宋:新的ReactiveVar(),
songId:new ReactiveVar(),
init:函数(songId){
this.songId.set(songId);
this.computation=Tracker.autorun(u.bind(this.update,this));
},
更新:函数(){
var songId=this.songId.get();
Meteor.subscribe('song',songId);
this.song.set(Songs.findOne(songId));
},
停止:函数(){
this.computation.stop();
}
};
SongManager.init(oldSongId);
SongManager.songId.set(newSongId);
//经过足够的时间更新订阅并刷新跟踪器后:
var currentSong=SongManager.song.get();
console.log(currentSong.\u id==newSongId);//真的

我还为您添加了一种停止自动运行计算的方法,这样当不再需要自动运行计算时,它就不会继续在后台运行。请注意,由于订阅是在自动运行中运行的,因此当
songId
更改时,订阅将自动停止并重新启动。更新功能实际上将运行两次,但Meteor知道不发送两个相同的订阅请求。

问题在于您正在创建循环依赖关系。我建议为此使用
ReactiveVar
,而不是使用较低级别的依赖API

meteor add reactive-var
然后你可以这样做:

SongManager={
宋:新的ReactiveVar(),
songId:new ReactiveVar(),
init:函数(songId){
this.songId.set(songId);
this.computation=Tracker.autorun(u.bind(this.update,this));
},
更新:函数(){
var songId=this.songId.get();
Meteor.subscribe('song',songId);
this.song.set(Songs.findOne(songId));
},
停止:函数(){
this.computation.stop();
}
};
SongManager.init(oldSongId);
SongManager.songId.set(newSongId);
//经过足够的时间更新订阅并刷新跟踪器后:
var currentSong=SongManager.song.get();
console.log(currentSong.\u id==newSongId);//真的

我还为您添加了一种停止自动运行计算的方法,这样当不再需要自动运行计算时,它就不会继续在后台运行。请注意,由于订阅是在自动运行中运行的,因此当
songId
更改时,订阅将自动停止并重新启动。更新功能实际上会运行两次,但Meteor知道不要发送两个相同的订阅请求。

谢谢,这正是我应该做的。我刚刚意识到我的错误;应该定义getSongCursor:function(songId){return Songs.find({u id:songId})}谢谢,这正是我应该做的。我刚刚意识到我的错误;应该定义getSongCursor:function(songId){return Songs.find({u id:songId})}谢谢,这正是我应该做的。我刚刚意识到我的错误;应该定义getSongCursor:function(songId){return Songs.find({u id:songId})}谢谢,这正是我应该做的。我刚刚意识到我的错误;应该定义getSo