May 09, 2021 Ember.js Reference documents
The relationship before the .js described in the thirty-eighth definition model of the Introduction Guide to Ember. M ainly includes one-to-one, one-to-many, many-to-many relationships. However, the two related relationship models have not been introduced to update, delete and other operations.
Two new model classes are created for testing.
ember g model post
ember g model comment
// app/models/post.js
import DS from 'ember-data';
export default DS.Model.extend({
comments: DS.hasMany('comment')
});
// app/model/comment.js
import DS from 'ember-data';
export default DS.Model.extend({
post: DS.belongsTo('post')
});
Set the association, and the maintenance of the relationship is placed on multiple
comment
let post = this.store.peekRecord('post', 1);
let comment = this.store.createRecord('comment', {
post: post
});
comment.save();
After
post
is automatically
comment
(saving the
post
id
property value to
post
property).
Of course, you can set
post
from post.
For example, the following code:
let post = this.store.peekRecord('post', 1);
let comment = this.store.createRecord('comment', {
// 设置属性值
});
// 手动吧对象设置到post数组中。(post是多的一方,comments属性应该是保存关系的数组)
post.get('comments').pushObject(comment);
comment.save();
If you've learned the hibernate framework in Java, I'm sure you'll understand the code easily.
As you can
post
is one side of the same, and if it wants to maintain a relationship is not to save the
id
of
comment
associated with it to
comments
post
can associate
comments
an array.
comment
Updating an association is almost identical to creating an association. It is also the first to get the models that need to be associated in setting up their associations.
let post = this.store.peekRecord('post', 100);
let comment = this.store.peekRecord('comment', 1);
comment.set('psot', post); // 重新设置comment与post的关系
comment.save(); // 保存关联的关系
Assuming that
comment
post
is data with
id
1
it is now
comment
data with
id
of
post
association of
100
If
post
from the post side, you can do something like this:
let post = this.store.peekRecord('post', 100);
let comment this.store.peekRecord('comment', 1);
post.get('comments').pushObject(comment); // 设置关联
post.save(); // 保存关联
Since there is a new relationship, there will naturally be a delete association.
If you want to remove the association between the two models, you only need to set the associated
null
let comment = this.store.peekRecord('comment', 1);
comment.set('post', null); //解除关联关系
comment.save();
Of course, you can also remove associations from one side.
let post = this.store.peekRecord('post', 1);
let comment = this.store.peekRecord('comment', 1);
post.get('comments').removeObject(comment); // 从关联数组中移除comment
post.save();
Maintaining a relationship from one party is actually maintaining an associated array element.
Whenever Store changes the Handlebars template, the data displayed on the page is automatically updated, and Ember Data is automatically updated to the server at the appropriate time.
There is a maintenance of the relationship between models is introduced here, the relationship between them is maintained in only two ways, one is maintained by one party, the other is maintained by one party, the other is maintained by one side, compared to the maintenance of one party is simpler. But if you need to update the association of multiple records at once, it's more appropriate to use the second method (both for array operations).
The full code of the blog post
is placed in Github
(the blog post has been modified several times, and the code on the blog post may be different from the github code, but the impact is small!). I
f you think the blog post is a bit useful to you, please give me a star on the
star
project.
Yours is definitely the biggest motivation for me!!