AngularFire 0.82 - 如何查询非规范化数据? [英] AngularFire 0.82 - How to query denormalised data?

查看:20
本文介绍了AngularFire 0.82 - 如何查询非规范化数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题类似于 AngularFire - 如何查询非规范化数据? 但我确实想要一个使用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

并且列表的内容存储在:

and the lists' content is stored under:

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.

一个更好的答案可以包括将访问包装在 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

推荐答案

了解我们正在尝试解决的用例而不是您为问题选择的解决方案会有所帮助(一如既往)-- 通常有更简单的方法来解决问题集(参见 什么是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();

仅限角度

仅使用 angular 的方法是手动加载列表并为单个列表使用 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 版;上一个版本应该使用 snap.name()

有关更高级的操作以及创建您自己的指令或服务,请查看 此处的问答.虽然乍一看可能不会出现,但这是关于如何获取任何 Firebase 数据并将其转换为任何专有结构的非常彻底的讨论.

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.

这篇关于AngularFire 0.82 - 如何查询非规范化数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆