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