我有一个延伸的问题 question .
如果球员属于多个球队怎么办?
我有这个
杰森
"Players" : { "-YRHd4IjrjsBXx__B" : { "name" : "The best forward", "creationDate" : "2016-02-26 15:50:39", "teams" : { "-KAByPeIz4IjrjsBXx__B" : true, "-KEFPuCXcqOah_GJwsMCu" : true, "-KEwuQxvGpYTEJ7YQ58-l" : true, "-KKF8vPtf8J7cfqFh2PLm" : true }, }, etc... }
玩家服务.js
getPlayers: function(teamid) { var Players = {}; var teamsIndex = ref.child('teams/' + teamid + '/players/'); var playersIndex = ref.child('players/'); teamsIndex.on('child_added', function(snapshot) { var playerKey = snapshot.key; playersIndex.child(playerKey).on('value', function(playersnap){ $timeout(function() { console.log("key", playerKey); players[playerKey] = playersnap.val(); }); }); }); teamIndex.on('child_removed', function(snapshot) { $timeout(function(snapshot) { delete players[snapshot.key()]; }); }); return players; }
但它返回一个对象列表。我知道我可能会查询/更改firebase中的数据结构,并将其作为$firebaseArray返回,因为我更喜欢使用angularfire。
您通常根据想要检索数据的方式来构造数据。
根据我的理解(如果我错了,请纠正我),你希望得到一个团队中的所有球员。为此,我将使用此结构:
"Players": { "player1": {...}, "player2": {...}, "player3": {...} }, "Teams': { "team1": {...}, "team2": {...} }, "TeamPlayers" : { "team1": { "player1": true, "player2": true }, "team2": { "player1": true, "player3": true } }
或使用数组
"TeamPlayers" : { "team1": [ 0: "player1", 1: "player2" ] }