【问题标题】:Add fields to angularfire programatically and sync with Firebase以编程方式向 angularfire 添加字段并与 Firebase 同步
【发布时间】:2015-08-07 10:18:36
【问题描述】:

使用 AngularJS、AngularFire 和 Firebase,作为一个新用户,我在分布式代码库方面遇到了麻烦。我试图经常使用 Factory() 来重用代码并遵循测试的最佳实践。但是,我无法将数据添加到我的 AngularFire 对象和 Firebase。

给定以下工厂:

angular.module('MyApp').factory("ItemData", ["$firebaseObject", "$firebaseArray", "GetFireBaseObject",
    function($firebaseObject, $firebaseArray, GetFireBaseObject) {
        var ItemsRef = GetFireBaseObject.DataURL('Items/');
        return {
            AllItems: function() {
                return $firebaseArray(ItemsRef);
            },
            OneItem: function(ItemKey) {
                var OneItemRef = ItemsRef.child(ItemKey); 
                return $firebaseObject(OneItemRef);
            }
        };
    }
]);

我可以获取我的项目,我可以处理数据等...但是,我似乎无法将元素添加到对象,并将其添加到 Firebase。虽然屏幕会在我调用 AddQuantity/MinusQuantity 时更新数量,但我无法将此数据链接到 Firebase 并更新那里的记录。

angular.module('MyApp').controller('InventoryCtrl', ["$scope", "StoreData", "ItemData"
    function ($scope, StoreData, ItemData) {

        $scope.StoreList = StoresData.AllStores();
        $scope.SelectedStore = {};     // Store Information
        $scope.ItemList = {};          // Store Item List

        $scope.GetItemsForStore = function() {
            // StoreDate.OneStoreItems returns list of items in this store
            var ItemData = StoreItems.OneStoreItems($scope.SelectedStore.Key);  // $firebaseArray() returned

            for( var i = 0; i < ItemData.length; ++i)
            {
                var Item = ItemData[i];

                // Item Data is master item data (description, base price, ...)
                var OneItem = ItemsData.OneItem(Item.$id);  // Get Master by Key

                Item.Description = OneItem.Description;   // From Master
                Item.UnitPrice = OneItem.UnitPrice;
                Item.Quantity = 0;
                Item.UnitTotal = 0;
            }
            $scope.ItemList = ItemData;
        };

        $scope.AddQuantity = function(item) {
            if( ! item.Quantity ) {
                item.Quantity = 0;
            }
            ++item.Quantity;
        };

        $scope.MinusQuantity = function(item) {
            if( ! item.Quantity ) {
                item.Quantity = 0;
            }
            --item.Quantity;
            if(item.Quantity <= 0) {
                item.Quantity = 0;
            }
        };
    }
]);

来自 HTML 的片段

            <pre>{{ ItemList | json}}</pre>
            <div>
                <table class="table">
                    <thead>
                        <th>Product Code</th>
                        <th>Description</th>
                        <th>Qty</th>
                        <th>&nbsp;</th>
                        <th>&nbsp;</th>
                        <th>Extended</th>
                    </thead>
                    <tbody>
                        <tr ng-repeat="(Key, item) in ItemList">
                            <td>{{item.$id}}</td>
                            <td>{{item.Description}}</td>
                            <td>{{item.Quantity}}</td>
                            <td><button class="btn btn-success" ng-click="AddQuantity(item)">+</button></td>
                            <td><button class="btn btn-danger" ng-click="MinusQuantity(item)">-</button></td>
                            <td>{{item.UnitPrice | SLS_Currency}}</td>
                            <td>{{item.UnitTotal}}</td>
                        </tr>
                    </tbody>
                </table>
            </div>

我可以看到添加到元素中的 ItemList 对象的 Quantity 字段,但即使我使用按钮从 ng-repeat 传递“item”元素,这似乎是正确的参考,但我没有看到数据同步到 Firebase。

【问题讨论】:

  • 你加载你的$firebaseObject into OneItem, but then modify the fields on Item. var OneItem = ItemData.OneItem(Item.$id);` vs Item.Description = OneItem.Description;。修改OneItem上的属性,然后调用$save()就可以了。
  • @FrankvanPuffelen 我用 cmets 更新了代码,以更好地解释我想要做什么。数据是标准化的,每个商店可能有来自主项目列表的不同信息子集。 OneItem 调用是针对主项目的,因此我可以从中获取描述、价格等...对于我想要更新的工作列表。我不想更改主项目,但要更改商店项目。
  • 据我所知,问题仍然存在:您需要在某处的$firebaseObject 上调用$save 以保存您的更改。如果不是这样,你可以设置一个重现问题的 jsfiddle/jsbin 吗?
  • @FrankvanPuffelen 我将不得不尝试将其拆开并创建一个,因为我需要 Firebase 参考等...

标签: javascript angularjs firebase angularfire firebase-realtime-database


【解决方案1】:

我将工厂更改为服务,我相信这仍然可以通过工厂完成,但我必须添加 AngularFire 手册中的绑定功能,如图所示。下面的代码子集用于选择记录并进行添加。

angular.module('MyApp').service("ItemData", ["$firebaseObject", "$firebaseArray", "GetFireBaseObject",
    function($firebaseObject, $firebaseArray, GetFireBaseObject) {
        var ItemRef = GetFireBaseObject.DataURL('Items/');

        this.AddItem = function(Item) {
            var OneItemRef = ItemRef.child(Item.Key);
            OneItemRef.update(Item);
            return $firebaseObject(OneItemRef);
        };

        this.DeleteItem = function(ItemKey) {
            var OneItemRef = ItemRef.child(ItemKey);
            OneItemRef.remove();
        };      

        this.GetAllItems = function() {
            return $firebaseArray(ItemRef);
        };

        this.GetOneItem = function(ItemKey) {
            var OneItemRef = ItemRef.child(ItemKey);
            return $firebaseObject(OneItemRef);
        };
    }
]);

然后控制器必须做一个额外的绑定,这也可能在服务中完成:

angular.module('MyApp').controller('ItemCtrl', ["$scope", "ItemData", 
    function ($scope, ItemData) {

        $scope.Items = ItemData.GetAllItems();

        $scope.EditItem = function(Item) {
            var EditItem = ItemData.GetOneItem(Item.Key);
            // Bind here for real time updates - NOTE: Changes with each key
            EditItem.$bindTo($scope, "Item").then(
                function(unbind) {
                    $scope.ItemUnbind = unbind;
                }
            );
            $scope.Item.NewField = "ABC";     // Add extra field to Firebase
        };

        $scope.SaveItem = function(Item) {
            if( ! $scope.ItemEditMode )
            {
                $scope.Item = ItemData.AddItem(Item);
            }
        };
    }
]);

尽管如此,任何更改(例如每次按键)都将立即同步到 Firebase 数据中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 2013-06-17
    相关资源
    最近更新 更多