Reputation: 17053
I'd like to create a system that could have a ton of games:
var gamePath = new Firebase("https://x.firebaseio.com/games/");
var games = $firebaseObject(gamePath);
games.$bindTo($scope, "games").then(function() {
// ...
if (typeof $scope.games[gameId] == "undefined"){
$scope.games[gameId] = setupEverything(userId);
}
else {
// ...
}
});
I presume that as the list of games becomes longer, this solution becomes unscalable. I'd like to change the gamePath to user specify a "gameId", but then I'd end up with a null return value. What's the right angularFire pattern to accomplish this more elegantly?
** Note: the reason I'm using an object in the first place is so that I can conveniently route players to the same game by a gameId. This simple key-to-game object mapping is important, I think.
Upvotes: 0
Views: 397
Reputation: 598728
Having a $firebaseObject()
that refers to a list of object is an anti-pattern. If you're referring to a list of objects, use $firebaseArray()
.
If you want to add a new game to that list, either use $firebaseArray.$add()
on it or (if you want to control the game ids) just add the game to the Firebase location directly. Since AngularFire is built on top of the Firebase JavaScript SDK, they interoperate perfectly.
var gamesPath = new Firebase("https://x.firebaseio.com/games/");
$scope.games = $firebaseArray(gamesPath);
var myGamePath = gamesPath.child(gameId);
$scope.mygame = $firebaseObject(myGamePath).$loaded(function() {
if (!$scope.mygame.$value) {
myGamePath.set({
uid: userId,
startedAt: Firebase.ServerValue.TIMESTAMP
})
};
});
With this, the new game will immediately be added to $scope.games
and set to $scope.mygame
when set()
is called.
Upvotes: 1