如何让 angularfire 自动将 ng-grid onBlur 更改发送到 firebase? [英] how to make angularfire send ng-grid onBlur changes to firebase automatically?

查看:20
本文介绍了如何让 angularfire 自动将 ng-grid onBlur 更改发送到 firebase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自此处的后续问题:如何在 angularFire 0.5.0 和最新的 ng-grid 之间创建 3 路数据绑定? where Kato 很好地提供了一个解决方案:

This is a followup question from here: How to create 3 way data binding between angularFire 0.5.0 and latest ng-grid? where Kato nicely provided a solution:

var app = angular.module('app', ['firebase', 'ngGrid']);
var fb = new Firebase('https://nggrid-example.firebaseio-demo.com/');

app.controller('ctrl', function($firebase, $scope, orderByPriorityFilter) {
    $scope.data = $firebase(fb);
    $scope.myData = $firebase(fb);
    $scope.$watchCollection('data', function() {
       $scope.myData = orderByPriorityFilter($scope.data); 
    });
    $scope.gridOptions = { data: 'myData',enableCellEditOnFocus: true };
});

然而,这个解决方案可以很好地将数据放入我的 ng-grid,但如果我编辑任何单元格,更改不会自动应用于 firebase.

However, this solution puts data nicely into my ng-grid, but if I edit any of the cells, changes are not automatically applied to firebase.

我尝试了各种方法,但似乎都不起作用:

I have tried various ways, but none of them seem to work :

$scope.$on('ngGridEventEndCellEdit',function(value){
                    $scope.data.$set(value);
                });

没有.

我也尝试过 angularFire 事件处理程序,例如 .$on("change")

I have also tried angularFire event handlers such as .$on("change")

我正在创建的表格将由多个用户实时编辑.

Tables I am creating will be edited by multiple users in real time.

Kato 解决方案很好地将 firebase 对象转换为数组,但随后我还需要找到一种方法,如何将 ng-grid 中的编辑值转换为对象,以便我可以将它们即时存储在 firebase 中.

Kato solution nicely converts firebase objects into arrays, but then I also need to find a way how to convert edited values in ng-grid to objects so I can store them in firebase on the fly.

如果我将 ng-blur="updateEntity(col, row)" 添加到我的 editableCellTemplate: 然后我添加:

if I add ng-blur="updateEntity(col, row)" to my editableCellTemplate: and then I add this :

$scope.updateEntity = function(col,row){
                    console.log(row.entity);
                    console.log(col.field);
                };

控制台完美注销,但我也收到此错误:

console logs out perfectly, but I also get this error:

错误:Firebase.set 失败:第一个参数在属性targetScope"中包含无效键 ($id).键必须是非空字符串,并且不能包含."、#"、$"、/"、["或]"

附言仅当我也有此错误时才会收到此错误:

P.S. I get this error only if I have this as well:

$scope.$on('ngGridEventEndCellEdit',function(value){
                    $scope.data.$set(value);
                });

如果我没有这件作品,那么任何东西都不会保存到 firebase(或至少尝试过).

If I dont have this piece, then nothing is being saved to firebase (or atleast tried).

推荐答案

我终于找到解决方案了!!

I finally found the solution !!

所需要做的就是将这个:ng-blur="updateEntity(col, row)" 添加到 editableCellTemplate: 然后这个:

All that is needed to be done is adding this : ng-blur="updateEntity(col, row)" to editableCellTemplate: and then this:

$scope.updateEntity = function(col,row){
                    $scope.data.$save(row.entity.$id);
                };

现在这个:$scope.data.$set(row.entity.$id); 将不起作用,因为它会用 id 替换整个 FIREBASE DATABASE(所以一个数字,例如0").所以在使用之前一定要测试一下!:)

Now this: $scope.data.$set(row.entity.$id); will not work, because it will replace THE WHOLE OF YOUR FIREBASE DATABASE with id (so a number, for example "0"). So always test it before using it! :)

多啊!3路数据绑定!

这篇关于如何让 angularfire 自动将 ng-grid onBlur 更改发送到 firebase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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