I'm trying my hand at using Firebase and AngularFire for an app I am building. I am admittedly a newcomer to angular and Firebase, so any help would be appreciated.
I have the following database structure in Firebase:
- EPICS
- Epic 1
- Id
- Title, etc.
- Epic 2
- Id
- Title, etc.
- Epic 1
USERS
User 1
-activeepics
active epic 1
active epic 2
On the following page, I have a ng-repeat for each epic in the database. Each epic has a row of buttons, one of which is giving me trouble. If the current user has already started the epic (e.g. the epic appears in "activeepics" for that user), then the button should say "Tap Out." If the epic is not in the user's activeepics, then it should display "Start."
The button should do different things when it is clicked, depending on the button label. So if the button label says "Start," the app should add the epic into the current users "activeepics" and change the button label to "Tap Out." In contrast, if the button label is "Tap Out," the app should remove the epic from "activeepics" and change the button label back to "Start."
However, I am getting all sorts of weird behavior. The app displays the correct button labels when I use dummy id's such as 1 or 0. However, when I add an epic with a Firebase generated ID, all labels reset to "Start." I've also noticed that the app is looping through each epic multiple times (I entered a debug console.log for each epic displayed).
I think the problem may be that I have multiple promise objects after binding the Firebase data to $scope. Please help if you can!
Here is the code:
View
<div ng-repeat="(name,epic) in epics | filter:search | orderBy:order | filter:{category:category}" class="epic">
<div>
<div class="small-block-grid-3 epicActions">
<li><a href="#"> Remix </a></li>
<li><a href="#/share"> Share </a></li>
<li><a href="#" ng-click="handleLabel(name)" prevent> {{getLabel(name)}} </a></li>
</div>
</ul>
</div>
</div>
Controller
$scope.desiredUser = UserService.getCurrentUser();
var ref = new Firebase("https://epicly.firebaseio.com/epics");
angularFire(ref, $scope, 'epics');
var ref2 = new Firebase("https://epicly.firebaseio.com/users");
angularFire(ref2, $scope, 'users').then(function(){
for (var i = 0; i<$scope.users.length; i++){
if($scope.users[i].email === $scope.desiredUser){
$scope.currUser = $scope.users[i];
}
}
var ref3 = new Firebase("https://epicly.firebaseio.com/users/" + $scope.currUser.id + "/activeepics");
angularFire(ref3, $scope, 'activeEpics').then(function(){
console.log($scope.activeEpics);
$scope.getLabel = function(epic){
console.log(epic);
for(var i = 0; i < $scope.activeEpics.length; i++){
if ($scope.activeEpics[i].id === epic) {
return "Tap Out";
}
}
return "Start";
}
$scope.handleLabel = function(name){
var label = $scope.getLabel(name);
if(label === "Tap Out"){
//remove from active epics
} else {
//add to activeepics
$scope.toAdd = {"id": name}
$scope.activeEpics.push($scope.toAdd);
}
}
});
});
handleLabel()param is the same as thegetLabel()param (e.g.getLabel(epicName)andhandleLabel(epicName)instead ofgetLabel(epic)andhandleLabel(name)). - bennlich