I am trying to learn Firebase AngularFire, using the new methods $asObject or $asArray, along with a data structure recommended in the blog post Denormalizing Your Data is Normal .
Let's say I have data of Lists and Items, and I want to return all the Items contained on a specific List.
FIREBASE ROOT: has two locations: 1) lists, 2) items
{
"items" : {
"itemid1" : {
"isonlist" : {
"listid3" : true
},
"itemname" : "itemA"
},
"itemid2" : {
"isonlist" : {
"listid3" : true
},
"itemname" : "itemB"
},
"itemid3" : {
"itemname" : "itemC"
},
"itemid4" : {
"itemname" : "itemD"
}
},
"lists" : {
"listid1" : {
"listtitle" : "ListA"
},
"listid2" : {
"listtitle" : "ListB"
},
"listid3" : {
"listitems" : {
"itemid1" : true,
"itemid2" : true
},
"listtitle" : "ListC"
},
"listid4" : {
"listtitle" : "ListD"
}
}
}
Here is my code:
<html ng-app="appListsOfItems">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.19/angular.min.js"></script>
<script src="https://cdn.firebase.com/js/client/1.0.21/firebase.js"></script>
<script src="https://cdn.firebase.com/libs/angularfire/0.8.2/angularfire.min.js"></script>
</head>
<body ng-controller="myCtrl">
<h1>Lists of Items</h1>
<b>Write out the title of this list: </b>
<div ng-model="list">{{list.listtitle}}</div>
<b>Write each of the itemids for this list - (isonlist):</b>
<div ng-repeat="(lid, li) in list.listitems">
{{lid}} | {{li}}
</div>
<b>Write the itemname for each of the items for this list:</b>
<div ng-repeat="(iid, i) in items">
{{iid}} | {{i.$id}} | {{i.itemname}}
</div>
<script>
var app = angular.module("appListsOfItems", ["firebase"]);
app.controller("myCtrl",
function($scope, $firebase) {
// Create firebase reference to a specific list 'listid3'
var listRef = new Firebase("https://xxxxx.firebaseio.com/lists/listid3");
// Assign the firebase reference to an angularfire object using the new $asObject method
var objList = $firebase(listRef).$asObject();
//Bind the object to scope and give it a name using the new $bindTo method
objList.$bindTo($scope, "list");
console.log(objList);
// Get the item name of each of the items on the list 'listid3'
// !!! THIS IS NOT WHAT I WANT. THIS RETURNS ALL ITEMS.
var refItems = new Firebase("https://xxxxx.firebaseio.com/items");
var arrayItems = $firebase(refItems).$asArray();
$scope.items = arrayItems;
console.log(arrayItems);
}
);
</script>
</body>
</html>
This code renders:
Lists of Items
Write out the title of this list:
ListC
Write each of the itemids for this list - (isonlist):
itemid1 | true
itemid2 | true
Write the itemname for each of the items for this list:
0 | itemid1 | itemA
1 | itemid2 | itemB
2 | itemid3 | itemC
3 | itemid4 | itemD
===========================================
ISSUE: $scope.items = arrayItems returns ALL THE ITEMS in the "items" location.
QUESTION: How do write out the "itemname" for the items only contained on "listid3"
EXCEPTED RESULT (Write out the item name of each of the items on the list "listid3"):
0 | itemid1 | itemA
1 | itemid2 | itemB