DS Namespace packages/ember-data/lib/core.js:5
Defined in: packages/ember-data/lib/core.js:5
Module: ember-data
All Ember Data methods and functions are defined inside of this namespace.
attr
(type, options)
Attribute
DS.attr
defines an attribute on a DS.Model.
By default, attributes are passed through as-is, however you can specify an
optional type to have the value automatically transformed.
Ember Data ships with four basic transform types: string
, number
,
boolean
and date
. You can define your own transforms by subclassing
DS.Transform.
DS.attr
takes an optional hash as a second parameter, currently
supported options are:
defaultValue
: Pass a string or a function to be called to set the attribute to a default value if none is supplied.
Example
1 2 3 4 5 6 7 |
var attr = DS.attr; App.User = DS.Model.extend({ username: attr('string'), email: attr('string'), verified: attr('boolean', {defaultValue: false}) }); |
Parameters:
- type String
- the attribute type
- options Object
- a hash of options
Returns:
- Attribute
belongsTo
(type, options)
Ember.computed
DS.belongsTo
is used to define One-To-One and One-To-Many
relationships on a DS.Model.
DS.belongsTo
takes an optional hash as a second parameter, currently
supported options are:
async
: A boolean value used to explicitly declare this to be an async relationship.inverse
: A string used to identify the inverse property on a related model in a One-To-Many relationship. See Explicit Inverses
One-To-One
To declare a one-to-one relationship between two models, use
DS.belongsTo
:
1 2 3 4 5 6 7 |
App.User = DS.Model.extend({ profile: DS.belongsTo('profile') }); App.Profile = DS.Model.extend({ user: DS.belongsTo('user') }); |
One-To-Many
To declare a one-to-many relationship between two models, use
DS.belongsTo
in combination with DS.hasMany
, like this:
1 2 3 4 5 6 7 |
App.Post = DS.Model.extend({ comments: DS.hasMany('comment') }); App.Comment = DS.Model.extend({ post: DS.belongsTo('post') }); |
Parameters:
- type String or DS.Model
- the model type of the relationship
- options Object
- a hash of options
Returns:
- Ember.computed
- relationship
hasMany
(type, options)
Ember.computed
DS.hasMany
is used to define One-To-Many and Many-To-Many
relationships on a DS.Model.
DS.hasMany
takes an optional hash as a second parameter, currently
supported options are:
async
: A boolean value used to explicitly declare this to be an async relationship.inverse
: A string used to identify the inverse property on a related model.
One-To-Many
To declare a one-to-many relationship between two models, use
DS.belongsTo
in combination with DS.hasMany
, like this:
1 2 3 4 5 6 7 |
App.Post = DS.Model.extend({ comments: DS.hasMany('comment') }); App.Comment = DS.Model.extend({ post: DS.belongsTo('post') }); |
Many-To-Many
To declare a many-to-many relationship between two models, use
DS.hasMany
:
1 2 3 4 5 6 7 |
App.Post = DS.Model.extend({ tags: DS.hasMany('tag') }); App.Tag = DS.Model.extend({ posts: DS.hasMany('post') }); |
Explicit Inverses
Ember Data will do its best to discover which relationships map to
one another. In the one-to-many code above, for example, Ember Data
can figure out that changing the comments
relationship should update
the post
relationship on the inverse because post is the only
relationship to that model.
However, sometimes you may have multiple belongsTo
/hasManys
for the
same type. You can specify which property on the related model is
the inverse using DS.hasMany
's inverse
option:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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' }) }); |
You can also specify an inverse on a belongsTo
, which works how
you'd expect.
Parameters:
- type String or DS.Model
- the model type of the relationship
- options Object
- a hash of options
Returns:
- Ember.computed
- relationship