Firebase 防止范围内的项写入其他用户';s记录
在应用程序中只有一个用户的场景中,我成功地使用了AngularFire 现在我已经启动并运行了身份验证,我注意到在切换用户时,将Firebase 防止范围内的项写入其他用户';s记录,firebase,angularfire,Firebase,Angularfire,在应用程序中只有一个用户的场景中,我成功地使用了AngularFire 现在我已经启动并运行了身份验证,我注意到在切换用户时,将项分配给$scope.items是灾难性的,主要是因为$scope无法正确更新 直接从文档中读取 var ref = new Firebase('https://<my-firebase>.firebaseio.com/items'); angularFire(ref, $scope, 'items'); 顺便说一句,我使用auth.provider和au
项
分配给$scope.items
是灾难性的,主要是因为$scope
无法正确更新
直接从文档中读取
var ref = new Firebase('https://<my-firebase>.firebaseio.com/items');
angularFire(ref, $scope, 'items');
顺便说一句,我使用auth.provider
和auth.id
生成userId
。现在我的项目已经在user1
中命名了
var ref = new Firebase('https://<my-firebase>.firebaseio.com/items/[user1id]');
angularFire(ref, $scope, 'items');
问题
现在,如果我只是注销并以其他人的身份登录,神奇的是,该用户拥有eenie meenie miney
和moe
,因为$scope.items
占据了注销和登录之间的数组
我试图在注销事件上设置$scope.items=[]
,但这实际上会清空所有记录。我在拔头发。这是我在我的项目中需要做的0.001%,它占用了我整个周末的时间
更新新方法
$scope.create = function() {
$scope.selectedDevice = {
name: 'New Device',
userId: $scope.user.provider + $scope.user.id
};
return $scope.devices.push($scope.selectedDevice);
};
$scope.$on('angularFireAuth:login', function(evt, user) {
var promise, ref;
ref = new Firebase('https://mysite.firebaseio.com/users/' + (user.provider + user.id) + '/registry/');
promise = angularFire(ref, $scope, 'devices');
});
它现在将准确地在用户id下创建项目。但是,一旦您注销并重新登录,这些项目不会从$scope.devices
中清除。因此,它们只是将自己添加到数据中,但在新登录的用户下
更新
我做了很多尝试和错误。我可能将
$scope.devices
设置为[]
,并以各种可能的组合移动登录事件。最终起作用的是@hiattp在被接受的答案中的摆弄。这是在切换用户时隐式数据绑定保持不变的结果。如果新用户出现并创建一个新的绑定,它将考虑现有的数据是它应该被同化的局部变化(这就是为什么你看到原始用户的项目被添加到新用户),但是,如果您试图先清除它们而不释放绑定,那么您就是在含蓄地告诉Firebase从原始用户的项目列表中删除该数据(也不是您想要的)。因此,当您根据需要检测注销(或登录)事件时,需要释放数据绑定
angularFire
promise中的回调提供了一种“解除绑定”方法(请参阅和):
您的代码中存在一些可能导致其无法工作的特性,请记住,
unbind
不会为您清除本地集合。但是,为了让您知道它应该如何工作(并证明它确实工作),这里是。您需要在注销时取消绑定$scope.items。最好的方法是在$scope中保存承诺的unbind函数:
var ref=new Firebase('https://.firebaseio.com/items/[user1id]”;
angularFire(参考,$scope,'items')。然后(函数(解除绑定){
$scope.unbinitems=取消绑定;
});
$scope.$on('angularFireAuth:logout',function(){
$scope.unbinitems();
});
不,那不是真的。我只是想展示一下它的样子。我将在上面澄清。实际上不会有任何字面上的定义。我尝试了你上面的定义,但我担心这并不能解决问题。来自先前登录用户的项目只需将其自身添加到新项目中。我将在上面发布新代码。明白了。是的,静态数组把我甩了。我更新了答案,这应该会有帮助。我也尝试了你的第二个链接,但也没有解决问题。谢谢你费心这么做。太疯狂了,我的工作方式不是那样的。一定是有别的事情把事情搞砸了。我会再看看,如果我找到了,我会把它贴在这里给其他人看。谢谢,阿南特。我也试过了,但没有掷骰子。(以上更新)
$scope.create = function(item) {
$scope.items.push(item)
/* Pretend the user adds these from the interface.
[
{ name: 'eenie' },
{ name: 'meenie' },
{ name: 'miney' },
{ name: 'moe' }
]
*/
}
$scope.create = function() {
$scope.selectedDevice = {
name: 'New Device',
userId: $scope.user.provider + $scope.user.id
};
return $scope.devices.push($scope.selectedDevice);
};
$scope.$on('angularFireAuth:login', function(evt, user) {
var promise, ref;
ref = new Firebase('https://mysite.firebaseio.com/users/' + (user.provider + user.id) + '/registry/');
promise = angularFire(ref, $scope, 'devices');
});
var promise = angularFire(ref, $scope, 'items');
promise.then(function(unbind){
// Calling unbind() will disassociate $scope.items from Firebase
// and generally it's useful to add unbind to the $scope for future use.
});