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

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

问题描述



现在我的身份验证已经启动并正在运行,我注意到了,我注意到了在我的应用程序中有一个用户的情况下使用AngularFire的情况。切换用户时,分配项目 $ scope.items 是灾难性的,主要是由于

直接从文档中读取...

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

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

  var ref = new火力(的 'https://<我的-火力> .firebaseio.com /项目/用户id'); 
angularFire(ref,$ scope,'items');

使用<$ c $生成 userId c> auth.provider auth.id ,btw。现在我的项目被命名空间(比方说) 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 )
/ *假装用户从界面添加这些。

{name:'eenie'},
{name:'meenie'},
{name:'miney'},
{name:'moe '}
]
* /

}

问题



现在,如果我只是注销并登录为其他人,神奇的是,该用户有 eenie meenie miney moe 因为 $ scope.items 在注销和登录之间保存了数组。 p>

我试图在注销事件中设置 $ scope.items = [] ,但实际上清空了所有的记录。我正在拉我的头发。这是我在我的项目中需要做的0.001%,并且是整个周末。


$ b 更新新方法

  $ scope.create = function(){
$ scope.selectedDevice = {
name:'New Device',
userId:$ scope.user.provider + $ scope.user.id
};
返回$ scope.devices.push($ scope.selectedDevice);
};
$ b $ 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从原始用户的项目列表中删除该数据(也不是您想要的)。因此,当您检测到注销(或登录)事件时,您需要释放数据绑定。


$ b

angularFire中的回调 promise提供了一个unbind方法(请参阅此处此处):

  var promise = angularFire(ref,$ scope,'items'); 
promise.then(函数(取消绑定){
//调用unbind()将从Firebase
中分离$ scope.items,并且通常将未绑定添加到$ scope以备将来使用使用。
});

在代码中有几个特质可能导致它不起作用, code> unbind 不会为您清除本地集合。但是,如果你想知道它应该如何工作(并且证明它不工作),那么这里是 / 2 /rel =nofollow>小提琴


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

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.

Reading directly from the docs...

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

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');

I add items to $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' }
  ]
  */

}

The problem

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.

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.

Update New method

  $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');
  });

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.

Update

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.

解决方案

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.

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

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天全站免登陆