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

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

问题描述

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

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

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

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天全站免登陆