May 09, 2021 Ember.js Reference documents
A model is also a class that defines the properties and data behavior that are presented to the user. T
he definition of the model is very simple and only needs
to inherit DS. T
he Model
class is fine, or you can create it directly
using the Ember CLI
command.
For example, a
ember g model person
the command model ember g model
person
// app/models/person.js
import DS from 'ember-data';
export default DS.Model.extend({
});
This is an empty model that does not define any properties.
With the model class, you can
find
method to find the data.
The model class person defined
person
not yet have any properties, so add a few properties to the class below.
// app/models/person.js
import DS from 'ember-data';
export default DS.Model.extend({
firstName: DS.attr(),
lastName: DS.attr(),
birthday: DS.attr()
});
The above code defines three properties, but has not specified a type for the property, and the default
string
type. T
hese property names are consistent with the data
key
on the servers you connect to.
You can even define the calculated property
in the model.
// app/models/person.js
import DS from 'ember-data';
export default DS.Model.extend({
firstName: DS.attr(),
lastName: DS.attr(),
birthday: DS.attr(),
fullName: Ember.computed('firstName', 'lastName', function() {
return `${this.get('firstName')} ${this.get('lastName')}`;
})
});
This code defines a calculated property,
fullName
The model classes defined earlier are not specified property types and
string
which is obviously not
string
number
boolean
date
These types I think I should know without my explanation.
Not only can you specify the property type, you can also specify the default value of the property, specified in the second argument of the attr() method. For example, the following code:
// app/models/person.js
import DS from 'ember-data';
export default DS.Model.extend({
username: DS.attr('string'),
email: DS.attr('string'),
verified: DS.attr('boolean', { defaultValue: false }), //指定默认值是false
// 使用函数返回值作为默认值
createAt: DS.attr('string', { defaultValue(){ return new Date(); } })
});
As described in code comments, setting default values includes specifying directly or using function return values.
Ember's model also has a database-like association. I t's a lot simpler than the model of Ember, the replicated database, which includes one-to-one, one-to-many, many-to-many associations. This relationship is uniform with the database in the background.
Declares a one-to-one association using the DS.belongsTo setting. For example, the following two models.
// app/models/user.js
import DS from 'ember-data';
export default DS.Model.extend({
profile: DS.belongsTo(‘profile’);
});
// app/models/profile.js
import DS from ‘ember-data’;
export default DS.Model.extend({
user: DS.belongsTo(‘user’);
});
Declares a one-to-many association using DS.belongsTo (used by multiple parties) and DS.hasMany (used by a smaller party) settings. For example, the following two models.
// app/models/post.js
import DS from ‘ember-data’;
export default DS.Model.extend({
comments: DS.hasMany(‘comment’);
});
This model is one side. The following model is more than one side;
// app/models/comment.js
import DS from ‘ember-data’;
export default DS.Model.extend({
post: DS.belongsTo(‘post’);
});
This setting is very similar to Java's hibernate.
Declare a one-to-many association using the DS.hasMany setting. For example, the following two models.
// app/models/post.js
import DS from ‘ember-data’;
export default DS.Model.extend({
tags: DS.hasMany(‘tag’);
});
// app/model/tag.js
import DS from ‘ember-data’;
export default DS.Model.extend({
post: DS.hasMany(‘post’);
});
The many-to-many relationship settings all use DS.hasMany, but you don't need an "intermediate table", which is a little different from the many-to-many of the data, if the data is many-to-many, it's usually associated through an intermediate table.
Ember Data tries
to discover the association between two models, such as in the previous one-to-many
comment
post when the comment
post
because
comment
corresponds to only one
post
comment
to
post
However, sometimes there are multiple models associated with this in the same model.
At this point, you can specify its associated model at the reverse end with
DS.hasMany's
inverse
option:
// app/model/comment.js
import DS from 'ember-data';
export default DS.Model.extend({
onePost: DS.belongsTo(‘post’),
twoPost: DS.belongsTo(‘post’),
redPost: DS.belongsTo(‘post’),
bluePost: DS.belongsTo(‘post’)
});
Three posts are associated in one model at
post
same time.
// app/models/post.js
import DS from ‘ember-data’;
export default DS.Model.extend({
comments: hasMany(‘comment’, { inverse: ‘redPost’ });
});
The
comment
model is automatically
redPost
changes.
When you want to define a model of inverse relationships (one-to-one relationships of the model itself), you must explicitly specify the associated model using
inverse
If there is no reverse relationship,
inverse
value
null
// app/models/folder.js
import DS from ‘ember-data’;
export default DS.Model.extend({
children: DS.hasMany(‘folder’, { reverse: ‘parent’ });
parent: DS.hasMany(‘folder’, { reverse: ‘children’ });
});
A folder usually has a parent or child folder. A
t this point, both the parent folder and the child folder are models of the same type as they are. A
t this point you need
inverse
as shown in this code, "children..." this line of code means that the
children
is also
folder
parent folder. T
he same "parent..." line of code means that the model has
parent
and this property is
folder
itself is a sub-folder of this property.
For example, the structure of the following image:
This is a bit like a list in a data structure.
You can
children
of
parent
and parent as a pointer.
Set
inverse
directly to null if only the association does not have
null
// app/models/folder.js
import DS from ‘ember-data’;
export default DS.Model.extend({
parent: DS.belongsTo(‘folder’, { inverse: null });
});
// app/models/user.js
import DS from ‘ember-data’;
export default DS.Model.extend({
bestFriend: DS.belongsTo(‘folder’, { inverse: ‘bestFriend’ });
});v
This relationship is similar to two-way one-to-one in database setup design.
Some models may contain deeply nested data objects, which can be a nightmare if they are also defined using the associations above! I
n this case, it is best to define the data as a simple object, although adding some redundant data reduces the hierarchy. T
he other is to define nested data as the properties of the model (also increasing redundancy but reducing the level of nesting).
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!!