访问firebase中的嵌套唯一键值

时间:2015-08-07 01:23:04

标签: javascript angularjs firebase

我确定这是一个微不足道的问题,但似乎无法弄清楚如何访问此firebase阵列中的玩家ID。我需要能够访问所有玩家ID,并且如果登录时建立的当前users.id与其中一个玩家id的firebase阵列匹配,那么这些游戏将使用ng-repeat进行循环。我知道如何完成后者,我只是想知道在唯一身份证中访问玩家ID;希望这是有道理的。非常感谢任何帮助,谢谢。

Firebase

JS

(这是与我的问题相关的一些代码)

game.controller('games.controller', ['$scope', '$state', '$stateParams', 'Auth', '$firebaseArray','Fire', function ($scope, $state, $stateParams, auth, $firebaseArray, fire) {

  $scope.games = $firebaseArray(fire.child('games'));
  $scope.view = 'listView';

  $scope.setCurrentGame = function(game) {
    $scope.currentGame = game;
  };

  $scope.createGame = function() {
    if ($scope.format == 'Match Play') {
      $scope.skinAmount = 'DOES NOT APPLY';
      $scope.birdieAmount = 'DOES NOT APPLY';
    }
    $scope.games.$add({
      name: $scope.gameName,
      host: $scope.user.name,
      date: $scope.gameDate,
      location: {
        course: $scope.courseName,
        address: $scope.courseAddress
      },
      rules: {
        amount: $scope.gameAmount,
        perSkin: $scope.skinAmount,
        perBirdie: $scope.birdieAmount,
        format: $scope.format,
        holes : $scope.holes,
        time: $scope.time
      }
    })
    $state.go('games');
  };

  $scope.addPlayer = function(game) {
    $firebaseArray(fire.child('games').child(game.$id).child('players')).$add({
      id : $scope.user.id,
      name : $scope.user.name,
      email : $scope.user.email
    });
  }

  // swap DOM structure in games state
  $scope.changeView = function(view){
    $scope.view = view;
  }

}]);

1 个答案:

答案 0 :(得分:5)

You're violating two common Firebase rules here:

  1. if an entity has a natural, unique id, store it with that id as its key
  2. don't nest lists

I'm guessing #1 happened because you are storing the players using $firebaseArray.$add(). Instead of repeating myself, I'll list a few questions that deal with the same problem:

The nesting of lists is a common mistake. By having the players stored under each game, you can never load the data for a list of games, without also loading all players for all games. For this reason (and others) it is often better to store the nested list under its own top-level:

games
    -Juasdui9
        date:
        host: 
    -Jviuo732
        date:
        host: 
games_players
    -Juasdui9
       -J43as437y239
           id: "Simplelogin:4"
           name: "Anthony"
    -Jviuo732
       ....
users
相关问题