Using AngularJS, AngularFire and Firebase, as a new user, I am having trouble with a distributed code base. I am trying to use Factory() often to re-use code and follow best practices for tests. However, I am having trouble adding data to my AngularFire object, and Firebase.
Given the following factory:
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);
}
};
}
]);
I can get my item, I can work with the data, etc... However, I cannot seem to add an element to the object, and have it added to Firebase. While the screen will update the quantity when I call the AddQuantity/MinusQuantity, I cannot get this data to link with Firebase and update the records there.
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;
}
};
}
]);
Snippet from 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>
I can see the Quantity field added to the ItemList object in the element, but even though I am passing the 'item' element from the ng-repeat with the buttons, which seems to be the proper reference, I do not see the data sync to Firebase.
$firebaseObject intoOneItem, but then modify the fields onItem.var OneItem = ItemData.OneItem(Item.$id);` vsItem.Description = OneItem.Description;. Modify the properties onOneItemand then call$save()on it.$saveon a$firebaseObjectsomewhere to save your changes. If that's not it, can you set up a jsfiddle/jsbin that reproduces the problem?