防止范围内的项目写入不同用户的记录 [英] Prevent items in scope from writing to a different user's records

查看:19
本文介绍了防止范围内的项目写入不同用户的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中有一个用户的场景中,我成功地使用了 AngularFire.

I was having success with using AngularFire in a scenario where there is one user on my application.

现在我已经启动并运行了身份验证,我注意到在切换用户时将 items 分配给 $scope.items 是灾难性的,主要是由于 $scope 未能正确更新.

Now that I have authentication up and running, I'm noticing that assigning items to $scope.items is catastrophic when switching users, mainly due to the $scope failing to update correctly.

直接阅读文档...

var ref = new Firebase('https://<my-firebase>.firebaseio.com/items');
angularFire(ref, $scope, 'items');

我只需要这些是当前授权用户的 items.所以目前,我这样做(如果有更好的方法,请不要犹豫告诉我!)

I need these to be only the items of the currently authorized user. So currently, I do this (if there's a better way, don't hesitate to tell me!)

var ref = new Firebase('https://<my-firebase>.firebaseio.com/items/userId');
angularFire(ref, $scope, 'items');

我使用 auth.providerauth.id 生成 userId,顺便说一句.现在我的项目被命名为(比方说)user1

I generate userId using auth.provider and auth.id, btw. Now that my items are namespaced in (let's say) user1

var ref = new Firebase('https://<my-firebase>.firebaseio.com/items/[user1id]');
angularFire(ref, $scope, 'items');

我将项目添加到 $scope.items

$scope.create = function(item) {

  $scope.items.push(item)
  /* Pretend the user adds these from the interface.
  [
    { name: 'eenie' },
    { name: 'meenie' },
    { name: 'miney' },
    { name: 'moe' }
  ]
  */

}

问题

现在,如果我只是注销并以其他人的身份登录,那么神奇的是该用户拥有 eenie meenie mineymoe 因为 $scope.items保持退出和登录之间的数组.

Now if I just log out and login as someone else, magically that user has eenie meenie miney and moe because $scope.items held the array between logout and login.

我尝试在注销事件上设置 $scope.items = [] ,但这实际上清空了所有记录.我正在拔头发.这是我需要在我的项目中完成的工作的 0.001%,它占用了我整个周末.

I tried to set $scope.items = [] on logout event, but that actually empties all the records. I'm pulling my hair out. This is 0.001% of what I need to do in my project and it's taking my whole weekend.

更新新方法

  $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 中清除.因此,他们只是将自己添加到数据中,但在新登录的用户下.

It now will accurately create items under the user's id. However, still, once you logout and log back in, those items do not get cleared from $scope.devices. Therefore, they just add themselves to data but under the newly logged in user.

更新

我做了很多尝试和错误.我可能将 $scope.devices 设置为 [] 并在每个可能的组合中移动登录事件.最终有效的是@hiattp 在接受的答案中的小提琴.

I did a lot of trial and error. I probably set $scope.devices to [] and moved around login events in every possible combination. What eventually worked was @hiattp's fiddle in the accepted answer.

推荐答案

这是当您切换用户时隐式数据绑定保持不变的结果.如果新用户出现并创建新绑定,它会认为现有数据是它应该吸收的本地更改(这就是为什么您会看到原始用户的项目被添加到新用户),但如果您尝试清除它们首先不释放绑定,然后您隐含地告诉 Firebase 从原始用户的项目列表中删除该数据(也不是您想要的).因此需要在检测到注销(或登录)事件时根据需要释放数据绑定.

This is a result of the implicit data binding remaining intact as you switch users. If the new user shows up and creates a new binding, it will consider the existing data to be local changes that it should assimilate (that's why you see the original user's items being added to the new user), but if you try to clear them first without releasing the binding then you are implicitly telling Firebase to delete that data from the original user's item list (also not what you want). So you need to release the data bindings when you detect the logout (or login) events as needed.

angularFire 承诺中的回调提供了一个解除绑定"方法(参见 此处此处):

The callback in the angularFire promise provides an "unbind" method (see here and here):

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.
});

您的代码中存在一些可能导致其无法工作的特性,请记住 unbind 不会为您清除本地集合.但是为了让您了解它应该如何工作(并证明它确实工作),这里是 一把小提琴.

You have a few idiosyncrasies in your code that are likely causing it not to work, and remember that unbind won't clear the local collection for you. But just so you have an idea of how it should work (and to prove it does work) here is a fiddle.

这篇关于防止范围内的项目写入不同用户的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆