AngularFire 访问子元素方法 [英] AngularFire Accessing child element methods

查看:29
本文介绍了AngularFire 访问子元素方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来获取子元素的方法,而不是单独加载该元素.

I'm looking for a way to get the methods of a child element instead of loading that element separately.

假设我有一个 Post 模型,每个帖子都有评论.这就是我获得帖子模型的方式:

Let's say I have a Post model and each post has comments. This is how I get the post model:

var post = $firebase(new Firebase(FIREBASE_URL + "/posts/" + post_name)).$asObject();

每个帖子都有评论,因此我可以使用以下方法访问评论:

Each post has comments so I can access the comments using:

post.$loaded(function () {
  var comments = post.comments;
}

现在如何将新元素推送到评论,而不必从 Firebase 单独加载模型?我应该能够执行以下操作,但它不起作用:

Now how do I push a new element to comments without having to load the model separately from Firebase? I should be able to do the following but it doesn't work:

comments.$add({text: "Hi, there"});

有没有什么合适的方法可以获取子元素的 firebase 方法而不必返回服务器获取评论?

Is there any proper way to get the firebase methods of a child element without having to go back to the server and get the comments?

推荐答案

Firebase 只会从服务器加载数据一次.之后,您的 JavaScript 代码可以根据需要在其上构建任意数量的引用,而无需再次下载数据.

Firebase will load the data from the server only once. After that your JavaScript code can construct as many refs on it as you need, without downloading the data again.

但是 post.comments.$asArray() 将不起作用,因为您的帖子是一个普通的 JavaScript 对象(不再是 $firebase 同步).

But post.comments.$asArray() will not work, since your post is a plain JavaScript object (and not a $firebase sync anymore).

改为尝试:

var ref = new Firebase(FIREBASE_URL + "/posts/" + post_name);
var post = $firebase(ref).$asObject();
var comments = $firebase(ref.child("comments")).$asArray();

考虑替代数据结构

但您可能需要重新考虑您的数据结构.尽管 Firebase 是一个分层数据存储,但他们建议不要构建深度嵌套的层次结构.请参阅 https://www 上的避免构建巢穴.firebase.com/docs/web/guide/structuring-data.html

Consider an alternate data structure

But you may want to reconsider your data structure. Even though Firebase is a hierarchical data store, they recommend against building deeply nested hierarchies. See Avoid Building Nests on https://www.firebase.com/docs/web/guide/structuring-data.html

因为我们最多可以嵌套 32 层深的数据,所以很容易认为这应该是默认结构.但是,当我们为 Firebase 中的节点获取数据时,我们还会检索其所有子节点.因此,在实践中,最好尽可能保持扁平,就像构建 SQL 表一样.

Because we can nest data up to 32 levels deep, it's tempting to think that this should be the default structure. However, when we fetch data for a node in Firebase, we also retrieve all of its child nodes. Therefore, in practice, it's best to keep things as flat as possible, just as one would structure SQL tables.

因此,在您的情况下,这可能会导致有两个顶级元素 postscomments.

So in your case that could lead to having two top-level elements posts and comments.

root
  posts
    post1
    post2
  comments
    post1
      post1comment1
      post1comment2

然后您可以简单地加载帖子及其评论:

And you can then simply load the post and its comments with:

var root= new Firebase(FIREBASE_URL);
var post = $firebase(root.child("posts").child(post_name)).$asObject();
var comments = $firebase(root.child("comments").child(post_name)).$asArray();

这篇关于AngularFire 访问子元素方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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