注册Firebase Listener而不调用它
Register Firebase Listener Without Calling It?
是否可以在注册Firebase侦听器函数时不调用它?
例如:
this.gamestateURL.on('value', function(snapshot){
self.GameStateChangeEvent(snapshot);
});
GameStateChangeEvent函数在设置侦听器后立即启动。
谢谢。
不幸的是,没有。文档特别声明:
此事件将使用存储在此位置的初始数据触发一次,然后在每次数据更改时再次触发。传递给回调的DataSnapshot将用于调用on()的位置。直到所有内容都同步后,它才会触发。如果该位置没有数据,则将使用空的DataSnapshot触发它(val()将返回null)。
然而,你可以这样做:
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());
});
英文:
- 创建一个处理更新/添加的子项的函数
- 开始侦听在当前时间戳(自epoch以来的unix时间)之后添加的所有子级的
child_added
事件。这还假设您将时间戳存储在这些子项上 - 开始侦听任何更改的子级的CCD_ 2事件
- 获取ref的所有值一次以初始化数据
- 不确定您的用例是否需要处理"child_removed"或"child_moved"
相关文章:
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- 处理函数中的异步调用(Firebase)
- Firebase对ref调用.remove()正在删除所有父级
- 请等待异步操作 (firebase) 调用完成,然后再加载网页
- Angular 应用程序在异步调用 Firebase 后未更新(脏检查)
- Firebase 承诺 - Query.once 失败:使用 1 个参数调用.预计至少 2
- 变量在 Firebase 调用后无法访问
- AngularJS从控制器调用Firebase uid
- 如何使用firebase从调用第三方API
- AngularJS和Firebase认证在第二次点击之后调用路由
- Firebase:当父节点被移除时调用child_removed
- 不要在Firebase页面刷新时调用onDisconnect()
- Firebase Authenticatoin - onauthstatechange被多次调用
- 在JavaScript函数中调用firebase函数
- Firebase getRedirectResult在注销后被调用
- 如何确保firebase在调用' process.exit() '之前同步所有数据库
- firebase有办法每两周调用我的代码吗?日程安排
- 如何使用AngularFire调用Firebase JS方法
- 注册Firebase Listener而不调用它
- 如何检测Firebase服务器调用何时完成