Ember - 理解逆关系 [英] Ember - Understanding Inverse Relationships

查看:14
本文介绍了Ember - 理解逆关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了 Ember 页面上的入门指南后,我仍然有点困惑究竟是什么逆关系是,以及何时定义它们.我知道您在定义多个相同类型的关系时可能需要使用它们,但我发现该示例非常不清楚.这是文档中的示例:

After reading through the Getting Started guide on Ember's page, I'm still a bit confused what exactly an inverse relationship is, and when to define them. I understand that you will probably need to use them when defining multiple relationships of the same type, but I found the example to be very unclear. This is the sample from the docs:

var belongsTo = DS.belongsTo,
    hasMany = DS.hasMany;

App.Comment = DS.Model.extend({
  onePost: belongsTo('post'),
  twoPost: belongsTo('post'),
  redPost: belongsTo('post'),
  bluePost: belongsTo('post')
});


App.Post = DS.Model.extend({
  comments: hasMany('comment', {
    inverse: 'redPost'
  })
});

在这个例子中,为什么 redPost 被挑出来作为反面而不是其他类型的帖子之一?将 redPost 定义为反向如何区分它与其余部分?我也真的不明白为什么一个评论有多个帖子,这只会增加我的困惑.

In this example, why is the redPost singled out as the inverse instead of one of the other kinds of posts? How does defining redPost as the inverse differentiate it from the rest? I also don't really understand why a comment has multiple posts at all really, just adding to my confusion.

推荐答案

首先,这篇 帖子可能会稍微解释一下.这不是你的确切问题,但答案的原因是相似的.

First of all, this post might explain things a bit. It's not your exact question, but the reasons for the answer are similar.

但是,要清楚地了解什么是逆,您应该熟悉有向图.虽然不是很明显,但有向图推动了 belongsTohasMany 背后的想法.

But, to get a clear understanding of what inverses are, you should be familiar with directed graphs. While not immediately apparent, directed graphs are what fuel the idea behind belongsTo and hasMany.

但让我们回到细节.拿他们的例子来说,只是去掉一些东西,让它更真实.

But let's get back to specifics. Let's take their example, only eliminate some stuff to make it more realistic.

App.Post = DS.Model.extend({
    comments: DS.hasMany('comment', { inverse: 'post' })
});

App.Comment = DS.Model.extend({
    post: DS.belongsTo('post', { inverse: 'comments' })
});

这是一个更真实的例子.每个帖子可以有任意数量的评论,而一个评论必须只属于一个帖子.说得通.如您所见,这些倒数是相互引用的.但什么是逆呢?逆关系只是描述了两个节点之间的边在另一侧被称为什么.比如看这张图:

This is a more real world example. Each post can have any number of comments, while a comment must belong to exactly one post. Makes sense. As you can see, those inverses refer to each other. But what is an inverse? An inverse relationship just describes what the edge between two nodes is called on the other side. For example, look at this picture:

您会看到两个节点之间有一条边.从Post 对象的角度来看,边缘称为comments.如果您想通过该边连接节点,您可以调用 post.get('comments').但是从Comment 对象的角度来看,边缘被称为post.如果您想使用 Comment 对象通过该边连接节点,则必须调用 comment.get('post').这就是逆向.它描述了不同的对象如何引用相同的关系.不同的名称,相同的边缘.通过显式声明逆,您可以告诉一个对象other 对象将什么称为边.

You'll see two nodes with a single edge between them. From the perspective of a Post object, the edge is called comments. If you wanted get the node connected by that edge, you could call post.get('comments'). But from the perspective of the Comment object, the edge is called post. If you wanted to get the node connected by that edge using the Comment object, you'd have to call comment.get('post'). That's what an inverse is. It describes how different objects refer to the same relationship. Different name, same edge. By explicitly declaring the inverse, you tell one object what the other object calls the edge.

为什么这很重要?好吧,Ember-Data 需要知道这一点,以便它可以互惠关系.例如,假设您有以下代码:

Why is that important? Well, Ember-Data needs to know that so it can reciprocate relationships. For example, let's say you have the following code:

var post = store.find('post', '1');
var newComment = store.createRecord('comment', {});

...

post.get('comments').addObject(newComment);

您所做的是创建一个新评论并将其连接到现有的帖子对象.一个足够简单的用例.但有一个问题:你只告诉帖子有关评论,反之亦然.你告诉帖子你把评论关联了,但你没有告诉评论你把它关联到帖子.但是作为用户,您希望这两者同时发生.嗯,他们这样做了,那是因为 Ember-Data 为您做到了.使用反向关系的名称,Ember-Data 确保当您执行一个操作时,另一个也会发生.当你这样做时:

What you did is create a new comment and connect it to an existing post object. A simple enough use case. But there's one problem: you only told the post about the comment, not vice-versa. You told the post that you connected the comment, but you didn't tell the comment that you connected it to the post. But as a user, you expect those both to happen at the same time. Well, they do, and that's because Ember-Data does it for you. Using the names of the inverse relationship, Ember-Data ensures that when you do one, the other happens as well. When you do this:

post.get('comments').addObject(newComment);

Ember-Data 在幕后确实做到了这一点:

Ember-Data, behind the scenes, really does this:

post.get('comments').addObject(newComment);
newComment.set('post', post);

就是 Ember-Data 需要反向关系的原因:这样它就可以保持完整性而无需您担心.

That is why Ember-Data needs the inverse relationship: so it can maintain integrity without you having to worry about it.

这篇关于Ember - 理解逆关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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