【问题标题】:Getting a firebase list from an array of indexes从索引数组中获取 firebase 列表
【发布时间】:2013-04-25 22:15:43
【问题描述】:

我有一个有序故事的索引:

{
  index: ["storyA", "storyB", "storyC"]
}

我想获取引用的故事并将其传递给 angularFireCollection

{
  index: ["storyA", "storyB", "storyC"],
  stories: {
    "storyA" : {},
    "storyB" : {},
    "storyC" : {},
    "storyD" : {},
    "storyE" : {}
  },
}

这是来自 angularfire 的示例。这将获得所有故事,而不仅仅是索引中的故事。如何只获取索引中的那些?

app.controller('stories', ['$scope', '$timeout', 'angularFireCollection',
  function($scope, $timeout, angularFireCollection) {
    var url = 'https://example.firebaseio.com/stories';
    $scope.stories = angularFireCollection(url);
  }
]);

【问题讨论】:

  • 只过滤它们是否可以接受?
  • 是的,过滤可能是您最好的选择,没有简单的方法只获取索引中的那些。或者,您可以为每个故事创建多个 angularFire 或 angularFireCollection 实例。例如:$scope.storyA = angularFireCollection('example.firebaseio.com/stories/storyA');
  • @lucuma 我想让索引工作。这是使用 firebase 的一个非常重要的部分。

标签: angularjs firebase angularfire


【解决方案1】:

Harry,查看FirebaseIndex 了解如何使用索引的示例。您使用 Firebase 所做的几乎所有工作都将涉及记录的索引或规范化(通过 id 匹配两组记录),因此这是一个很好的研究主题。

索引不适用于开箱即用的 AngularFire。但是,可以提交一点想法和拉取请求以允许它直接与 FirebaseIndex 一起使用(只需传入 FirebaseIndex 而不是 url,并在 AngularFire 构造函数中,将 this._fref 设置为 FirebaseIndex 而不是 new Firebase(url)

例子:

function AngularFire($q, $parse, url) {
  this._q = $q;
  this._parse = $parse;
  this._initial = true;
  this._remoteValue = false;
  this._fRef = typeof(url) === 'string'? new Firebase(url) : url;
}

但它可能没那么简单(是什么?)。这可能仅限于只读范围。要在两个方向上完成这项工作需要更多的计划和工作。

【讨论】:

  • 感谢 FirebaseIndex!我现在只是尝试做单向。我不认为仅仅设置 this._fref 就足够了。传入 FirebaseIndex 会报错:Error: new Firebase failed: First argument must be a valid firebase URL and the path can't contain ".", "#", "$", "[", or "]". Trying to figure out.
  • Harry,你需要对 AngularFire 进行修改;以上并不是一个就地解决方案,而是一个关于如何实现你想要的东西的想法。查看我的编辑。
猜你喜欢
  • 2020-06-19
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 2013-03-30
  • 2022-06-29
  • 2013-04-29
  • 1970-01-01
相关资源
最近更新 更多