如何通过两个不同的索引访问相同的Firebase数据



我正在尝试构建一个团队运动时间表的交互式矩阵,其中行是每个团队,列是每周。这是一个幸存者池,用户每周只能选择一个赢家,每个团队每年只能选择一次。因此,当用户点击一个给定的游戏时,相关的行列应该被设置为非活动。我是noSQL的新手,所以我不确定如何与团队和周的游戏互动。

由于我使用Firebase作为后端,我开始使用.json文件结构,teams$firebaseArray每个schedule嵌套如下:

{
"teams": {
"California": {
  "slug": "california",
  "chosen": false,
  "schedule": [
    {
      "week": 1,
      "opponent": "Grambling St",
      "selected": false,
      "available": true
    },
    {
      "week": 2,
      "opponent": "San Diego St",
      "selected": false,
      "available": true
    },
    {
      "week": 3,
      "opponent": "at Texas",
      "selected": false,
      "available": true
    },
    {
      "week": 4,
      "opponent": "at Washington",
      "selected": false,
      "available": true
    },
    {
      "week": 5,
      "opponent": "Washington St",
      "selected": false,
      "available": true
    },
    {
      "week": 6,
      "opponent": "at Utah",
      "selected": false,
      "available": true
    },
    {
      "week": 7,
      "opponent": "Bye",
      "selected": false,
      "available": true
    },
    {
      "week": 8,
      "opponent": "at UCLA",
      "selected": false,
      "available": true
    },
    {
      "week": 9,
      "opponent": "USC",
      "selected": false,
      "available": true
    },
    {
      "week": 10,
      "opponent": "at Oregon",
      "selected": false,
      "available": true
    },
    {
      "week": 11,
      "opponent": "Oregon St",
      "selected": false,
      "available": true
    },
    {
      "week": 12,
      "opponent": "at Stanford",
      "selected": false,
      "available": true
    },
    {
      "week": 13,
      "opponent": "Arizona St",
      "selected": false,
      "available": true
    }
  ]
},
"Oregon": {
  "slug": "oregon",
  "chosen": false,
  "schedule": [
    {
      "week": 1,
      "opponent": "EWU",
      "selected": false,
      "available": true
    },
    {
      "week": 2,
      "opponent": "at Michigan St",
      "selected": false,
      "available": true
    },
    {
      "week": 3,
      "opponent": "Georgia St",
      "selected": false,
      "available": true
    },
    {
      "week": 4,
      "opponent": "Utah",
      "selected": false,
      "available": true
    },
    {
      "week": 5,
      "opponent": "at Colorado",
      "selected": false,
      "available": true
    },
    {
      "week": 6,
      "opponent": "Washington St",
      "selected": false,
      "available": true
    },
    {
      "week": 7,
      "opponent": "at Washington",
      "selected": false,
      "available": true
    },
    {
      "week": 8,
      "opponent": "Bye",
      "selected": false,
      "available": true
    },
    {
      "week": 9,
      "opponent": "at Arizona St",
      "selected": false,
      "available": true
    },
    {
      "week": 10,
      "opponent": "California",
      "selected": false,
      "available": true
    },
    {
      "week": 11,
      "opponent": "at Stanford",
      "selected": false,
      "available": true
    },
    {
      "week": 12,
      "opponent": "USC",
      "selected": false,
      "available": true
    },
    {
      "week": 13,
      "opponent": "Oregon St",
      "selected": false,
      "available": true
    }
  ]
},

我可以很容易地通过ng-repeat="team in teams"在行级别和game in team.scheduletd级别构建表。设置相关的team.chosentrue标记行不活动,但我不确定如何按周(列)做同样的事情。

========

我决定用不同的数据结构再试一次,这次把gamesteams中分离出来,像这样:

{
"teams": {
"California": {
  "chosen": false,
  "slug": "california"
},
"Oregon": {
  "chosen": false,
  "slug": "oregon"
},
"Oregon St": {
  "chosen": false,
  "slug": "oregon-st"
},
"Stanford": {
  "chosen": false,
  "slug": "stanford"
},
"Washington": {
  "chosen": false,
  "slug": "washington"
},
"Washington St": {
  "chosen": false,
  "slug": "washington-st"
}
},
"games": {
"one": {
  "Stanford": "at Northwestern",
  "Washington": "at Boise St",
  "California": "Grambling St",
  "Oregon": "EWU",
  "Oregon St": "Weber St",
  "Washington St": "Portland St"
},
"two": {
  "Stanford": "UCF",
  "Washington": "Sacramento St",
  "California": "San Diego St",
  "Oregon": "at Michigan St",
  "Oregon St": "at Michigan",
  "Washington": "at Rutgers"
},
"three": {
  "Stanford": "at USC",
  "Washington": "Utah St",
  "California": "at Texas",
  "Oregon": "Georgia St",
  "Oregon St": "San Jose St",
  "Washington": "Wyoming"
}
}
}

这并没有得到更好的结果,因为我无法在weeks的ng重复中达到游戏对象的值。

我怀疑我在Firebase中构造这个数据时遗漏了什么。

随机部分是用户点击的矩阵单元格(他们选择了什么),所以如何跟踪每个选择。孩子们将得到谁摘了它的user_id,周#(列)和team_id(行)。

pick_id_x将是一个随机生成的Firebase节点名称(childByAutoId)

picks_node:
    pick_id_0:
      user: user_id
      week: week #
      team: team_id
    pick_id_1:
      user: user_id
      week: week #
      team: team_id

使用此结构,您可以查询周,团队或用户,以及出现在拾取节点中的任何内容都将被拾取,因此无法再次拾取(您将在UI中解决此问题)。因此,您从源数据中填充矩阵行和列,然后加载这个选择列表并'屏蔽'每周和团队。

你可以用这个来回答"我想知道哪个用户在第3周选择了哪个团队"或"哪个团队最受用户喜爱"的问题。

(我使用team_id,但如果你想保留更多的游戏数据,它也可以是game: game_id_x)

games_node:
  game_id_0:
    t1: home_team
    t2: visiting_team
  game_id_1:
    t1: home_team
    t2: visiting_team

请注意,我将数据与它的键(节点名,引用,随便你怎么称呼它)解除了关联。

一般Firebase:越平越好,不要硬编码绝对数据

相关内容

  • 没有找到相关文章

最新更新