附加到 <firebase-ref>.onDisconnect.remove() 的回调何时触发?
When does the callback attached to <firebase-ref>.onDisconnect.remove() fire?
我正在使用 AngularJS 和 Firebase 构建网络应用程序。 Firebase 文档说可以将可选的回调附加到 onDisconnect().remove()。我预计此回调仅在删除事件完成时触发。但是,它会在附加回调后立即触发。我按如下方式附加回调:
var ref = new Firebase("some-ref-url");
ref.onDisconnect().remove(function(err) {
console.log("remove callback firing");
});
ref.set(true);
有没有办法确保回调仅在用户断开连接时触发,而不是在连接后立即触发?
基于我打算如何使用 onDisconnect().remove() 的最小工作示例:
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
<script src="https://cdn.firebase.com/js/client/2.2.7/firebase.js"></script>
<script src="https://cdn.firebase.com/libs/angularfire/1.1.2/angularfire.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">{{hw}}</div>
<script>
var app = angular.module('myApp', ['firebase']);
app.controller('myCtrl', function($firebase, $scope) {
var ref1 = new Firebase("https://<my-app>.firebaseio.com/test1");
var ref2 = new Firebase("https://<my-app>.firebaseio.com/test2");
ref1.onDisconnect().remove(function(){
console.log('remove callback firing');
ref2.set(true);
});
$scope.hw = "Hello, World!";
});
</script>
</body>
</html>
在上面的代码中,ref2 在页面加载时设置为 true(而不是在用户断开连接时)。如何确保仅在用户断开连接时才将 ref2 设置为 true?任何帮助表示赞赏。提前致谢。
当 Firebase 服务器检测到用户已断开连接时,您附加到 onDisconnect()
的处理程序会在 上运行 。在那个阶段,完成处理程序无法再在您的客户端上触发。
在这种情况下,最好将逻辑一分为二:
- what needs to happen on the server when the user gets disconnected?
- what needs to happen on the client when the user gets disconnected?
第一个由 onDisconnect()
处理程序处理,后者由 .info/connected
值侦听器处理。我已经链接到上面每个的编程指南。
我正在使用 AngularJS 和 Firebase 构建网络应用程序。 Firebase 文档说可以将可选的回调附加到 onDisconnect().remove()。我预计此回调仅在删除事件完成时触发。但是,它会在附加回调后立即触发。我按如下方式附加回调:
var ref = new Firebase("some-ref-url");
ref.onDisconnect().remove(function(err) {
console.log("remove callback firing");
});
ref.set(true);
有没有办法确保回调仅在用户断开连接时触发,而不是在连接后立即触发?
基于我打算如何使用 onDisconnect().remove() 的最小工作示例:
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
<script src="https://cdn.firebase.com/js/client/2.2.7/firebase.js"></script>
<script src="https://cdn.firebase.com/libs/angularfire/1.1.2/angularfire.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">{{hw}}</div>
<script>
var app = angular.module('myApp', ['firebase']);
app.controller('myCtrl', function($firebase, $scope) {
var ref1 = new Firebase("https://<my-app>.firebaseio.com/test1");
var ref2 = new Firebase("https://<my-app>.firebaseio.com/test2");
ref1.onDisconnect().remove(function(){
console.log('remove callback firing');
ref2.set(true);
});
$scope.hw = "Hello, World!";
});
</script>
</body>
</html>
在上面的代码中,ref2 在页面加载时设置为 true(而不是在用户断开连接时)。如何确保仅在用户断开连接时才将 ref2 设置为 true?任何帮助表示赞赏。提前致谢。
当 Firebase 服务器检测到用户已断开连接时,您附加到 onDisconnect()
的处理程序会在 上运行 。在那个阶段,完成处理程序无法再在您的客户端上触发。
在这种情况下,最好将逻辑一分为二:
- what needs to happen on the server when the user gets disconnected?
- what needs to happen on the client when the user gets disconnected?
第一个由 onDisconnect()
处理程序处理,后者由 .info/connected
值侦听器处理。我已经链接到上面每个的编程指南。