Ember - 了解反向关系 [英] Ember - Understanding Inverse Relationships

查看:100
本文介绍了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.

但是,为了更清楚地了解什么是反转,你应该熟悉有向图。虽然不是很明显,但是有针对性的图表是对 belongsTo hasMany 背后的想法的贡献。

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 对象的角度来看,边缘被称为评论。如果你想让这个边缘连接的节点,你可以调用 post.get('comments')。但是从 Comment 对象的角度来看,边缘称为 post 。如果要使用 Comment 对象获得该边缘连接的节点,则必须调用 comment.get('post')。这是一个倒数。它描述不同的对象如何指向相同的关系。不同名称,同一边。通过明确声明反向,您会告诉一个对象其他对象调用边缘。

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);

你所做的是创建一个新的注释并将其连接到现有的post对象。一个简单的用例。但是有一个问题:你只是在发表评论的时候,反之亦然。你告诉过这个帖子你连接了这个注释,但是你没有告诉你把它连接到帖子。但是,作为用户,您期望同时发生这两者。那么他们这样做,那是因为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天全站免登陆