2016-07-21 39 views
0

注册Firebase侦听器函数时是否可以在注册时不调用它?注册Firebase侦听器而不调用它?

例如:立即

this.gamestateURL.on('value', function(snapshot){ 
    self.GameStateChangeEvent(snapshot); 
}); 

GameStateChangeEvent功能火灾取决于建立的监听器。

谢谢。

+0

这听起来像一个XY问题。你想达到什么目的? –

回答

0

不幸的是,没有。 docs具体说明:

该事件将触发一次,初始数据存储在此位置,然后每次数据更改时再次触发。传递给回调的DataSnapshot将用于调用on()的位置。在整个内容被同步之前它不会被触发。如果该位置没有数据,则会使用空的DataSnapshot触发(val()将返回空值)。

你可以,但是做这样的事情:

var ref = this.gamestateURL // or however you create a ref 

function doSomethingWithAddedOrChangedSnapshot(snapshot) { 
    // this function is called whenever a child is added 
    // or changed 
} 

// assuming you have "timestamp" property on these objects 
// it will not be called back with any snapshots on initialization 
// because the timestamp of existing snapshots will not be greater 
// than the current time 
ref.orderByChild('timestamp') 
    .startAt(new Date().getTime()) 
    .on('child_added', doSomethingWithAddedOrChangedSnapshot); 

ref.on('child_changed', doSomethingWithAddedOrChangedSnapshot); 

ref.once('value', function(snapshot){ 
    // get the initial state once 
    // this snapshot represents all the items on this ref 
    // this will only fire once on initialization 
    initializeGameData(snapshot.val()); 
}); 

英文:

  • 创建一个函数来处理更新/添加的子
  • 开始收听child_added事件在当前时间戳之后添加的所有子项(从历元开始的unix时间)。这也假设你正在存储这些孩子的时间戳。
  • 开始聆听child_changed事件的任何更改的孩子。
  • 抓住ref的所有值来初始化您的数据。
  • 不知道,如果你的使用情况需要处理“child_removed”或“child_moved”
相关问题