AngularFire 0.82 - 如何查询denormalised数据?数据、AngularFire、denormalised

由网友(百依百顺敌不过她忽冷忽热)分享简介:此问题是类似 AngularFire - 如何查询denormalised数据,但我真的想使用一个答案? AngularFire (当前版本是0.82)。这是数据结构我用一个例子:This question is similar to AngularFire - How do I query denormalis...

此问题是类似 AngularFire - 如何查询denormalised数据,但我真的想使用一个答案? AngularFire (当前版本是0.82)。这是数据结构我用一个例子:

This question is similar to AngularFire - How do I query denormalised data? but I really do want an answer that uses AngularFire (current version is 0.82). This is an example of the data structure I use:

{
  "users": {
    "user1": {
      "name": "Alice",
      "lists": {
        "list1": "true"
      }
    },
    "user2": {
      "name": "Bob",
      "lists": {
        "list2": "true"
      }
    }
  },
  "lists": {
    "list1": {
      "title": "Example"
    },
    "list2": {
      "title": "Example2"
    }
  }
}

或者,在不同的符号:让所有的一个用户的名单,我们需要所有这些:

or, in a different notation: to get all of one user's lists we need all of these:

users/$userID/lists/$listID

和列表内容下保存:

lists/$listID

我想实现的是能够访问特定用户的列表的内容,但没有手动遍历每个$ ListID。

What I want to achieve is to be able to access the content of a specific user's lists, but without "manually" iterating over each $ListID.

这是更令人满意的回答可以包括code,它包装的访问在AngularFire超期服役,类似code。在创建AngularFire服务在 AngularFire文档

An even nicer answer can include code that wraps that access in an AngularFire extended service, similar to the code under "Creating AngularFire Services" at AngularFire docs

推荐答案

这将有助于(因为它总是)了解我们正在努力解决这个用例,而不是简单的你找的problem-解决方案-there往往是简单的方法来解决这个问题集(见什么XY问题?)。

It would help (as it always does) to understand the use case we're trying to resolve rather than simply the solution you've picked to the problem--there are often simpler ways to approach the problem set (see What is the XY problem?).

有什么内置AngularFire处理列表的嵌套列表。具体内容的清单实际上是比从列表中引用的几个列表轻松了不少。

There is nothing built into AngularFire to handle nested lists of lists. A list of specific elements would actually be quite a bit easier than several lists referenced from a list.

Firebase.util 的工具的存在是为了帮助非规范化(它目前得到加快转速来V2,在一个月或两个月),因为出来,它与AngularFire兼容:

A tool called Firebase.util exists to help with denormalization (it's currently getting revved to V2, due out in a month or two), and it's compatible with AngularFire:

var fbRef = new Firebase(URL);
var indexRef = fbRef.child('users/' + userId + '/lists');
var dataRef = fbRef.child('lists');
var joinedRef = Firebase.util.intersection(indexRef, dataRef);

var lists = $firebase( joinedRef ).$asArray();

只角

这是角只方法是用手工加载列表和利用AngularFire为单个列表

Angular only

An angular-only approach would be to load the lists by hand and utilize AngularFire for the individual lists:

app.factory('NestedList', function($firebase, $timeout) {
   return function(indexRef, dataRef) {
      var hashOfLists = {};
      indexRef.on('child_added', function(snap) {
         var key = snap.key();
         var list = $firebase( dataRef.child(key).$asArray();
         hashOfLists[key] = list;
      });
      indexRef.on('child_removed', function(snap) {
         $timeout(function() {
            delete hashOfLists[snap.key()];
         });
      });
      return hashOfLists;
   }
});

注意,这个例子使用snap.key(),但只使用SDK,这是就在本周发布的2.x版本时适用; $ P $光伏版本应该使用snap.name()

有关更高级的操作,并创建自己的指令或服务,请查看question并回答这里。虽然它可能不是第一眼看起来,这是一个如何采取任何火力地堡数据,并将其转换成任何所有权结构的pretty深入的讨论。

For more advanced operations and to create your own directives or services, check out the question and answer here. Although it may not appear at first glance, this is a pretty thorough discussion of how to take any Firebase data and transform it into any proprietary structure.

阅读全文

相关推荐

最新文章