Class Model
publicBase class from which Models can be defined.
1 2 3 |
import Model, { attr } from ' -data/model'; export default class User extends Model { |
belongsTo (name) BelongsToReference public
Defined in ../packages/model/src/-private/model.js:1003
Available since v2.5.0
- name
- String
- of the relationship
- returns
- BelongsToReference
- reference for this relationship
Get the reference for the specified belongsTo relationship.
Example
app/models/blog.js | |
1 2 3 |
import Model, { belongsTo } from ' -data/model'; export default class BlogModel extends Model { |
changedAttributes Object public
Defined in ../packages/model/src/-private/model.js:784
- returns
- Object
- an object, whose keys are changed properties, and value is an [oldProp, newProp] array.
Returns an object, whose keys are changed properties, and value is an [oldProp, newProp] array.
The array represents the diff of the canonical state with the local state of the model. Note: if the model is created locally, the canonical state is empty since the adapter hasn't acknowledged the attributes yet:
Example
app/models/mascot.js | |
1 2 3 |
import Model, { attr } from ' -data/model'; export default class MascotModel extends Model { |
deleteRecord public
Defined in ../packages/model/src/-private/model.js:654
Marks the record as deleted but does not save it. You must call
save
afterwards if you want to persist it. You might use this
method if you want to allow the user to still rollbackAttributes()
after a delete was made.
Example
app/controllers/model/delete.js | |
1 2 3 4 |
import Controller from ' /controller'; import { action } from ' /object'; export default class ModelDeleteController extends Controller { |
destroyRecord (options) Promise public
Defined in ../packages/model/src/-private/model.js:694
- options
- Object
- returns
- Promise
- a promise that will be resolved when the adapter returns successfully or rejected if the adapter returns with an error.
Same as deleteRecord
, but saves the record immediately.
Example
app/controllers/model/delete.js | |
1 2 3 4 |
import Controller from ' /controller'; import { action } from ' /object'; export default class ModelDeleteController extends Controller { |
eachAttribute (callback, binding) public
Defined in ../packages/model/src/-private/model.js:2228
- callback
- Function
- The callback to execute
- binding
- Object
- the value to which the callback's `this` should be bound
Iterates through the attributes of the model, calling the passed function on each attribute.
The callback method you provide should have the following signature (all parameters are optional):
1 |
function(name, meta); |
name
the name of the current property in the iterationmeta
the meta object for the attribute property in the iteration
Note that in addition to a callback, you can also pass an optional target
object that will be set as this
on the context.
Example
1 2 3 |
import Model, { attr } from ' -data/model'; class PersonModel extends Model { |
eachRelatedType (callback, binding) public
Defined in ../packages/model/src/-private/model.js:2007
- callback
- Function
- the callback to invoke
- binding
- Any
- the value to which the callback's `this` should be bound
Given a callback, iterates over each of the types related to a model, invoking the callback with the related type's class. Each type will be returned just once, regardless of how many different relationships it has with a model.
eachRelationship (callback, binding) public
Defined in ../packages/model/src/-private/model.js:1134
- callback
- Function
- the callback to invoke
- binding
- Any
- the value to which the callback's `this` should be bound
Given a callback, iterates over each of the relationships in the model, invoking the callback with the name of each relationship and its relationship descriptor.
The callback method you provide should have the following signature (all parameters are optional):
1 |
function(name, descriptor); |
name
the name of the current property in the iterationdescriptor
the meta object that describes this relationship
The relationship descriptor argument is an object with the following properties.
- key String the name of this relationship on the Model
- kind String "hasMany" or "belongsTo"
- options Object the original options hash passed when the relationship was declared
- parentType Model the type of the Model that owns this relationship
- type String the type name of the related Model
Note that in addition to a callback, you can also pass an optional target
object that will be set as this
on the context.
Example
app/serializers/application.js | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import JSONSerializer from '@ember-data/serializer/json'; export default class ApplicationSerializer extends JSONSerializer { serialize(record, options) { let json = {}; record.eachRelationship(function(name, descriptor) { if (descriptor.kind === 'hasMany') { let serializedHasManyName = name.toUpperCase() + '_IDS'; json[serializedHasManyName] = record.get(name).map(r => r.id); } }); return json; } } |
eachTransformedAttribute (callback, binding) public
Defined in ../packages/model/src/-private/model.js:2295
- callback
- Function
- The callback to execute
- binding
- Object
- the value to which the callback's `this` should be bound
Iterates through the transformedAttributes of the model, calling the passed function on each attribute. Note the callback will not be called for any attributes that do not have an transformation type.
The callback method you provide should have the following signature (all parameters are optional):
1 |
function(name, type); |
name
the name of the current property in the iterationtype
a string containing the name of the type of transformed applied to the attribute
Note that in addition to a callback, you can also pass an optional target
object that will be set as this
on the context.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import Model, { attr } from '@ember-data/model'; let Person = Model.extend({ firstName: attr(), lastName: attr('string'), birthday: attr('date') }); Person.eachTransformedAttribute(function(name, type) { // do thing }); // prints: // lastName string // birthday date |
hasMany (name) HasManyReference public
Defined in ../packages/model/src/-private/model.js:1071
Available since v2.5.0
- name
- String
- of the relationship
- returns
- HasManyReference
- reference for this relationship
Get the reference for the specified hasMany relationship.
Example
app/models/blog.js | |
1 2 3 |
import Model, { hasMany } from ' -data/model'; export default class BlogModel extends Model { |
inverseFor (name, store) Object public
Defined in ../packages/model/src/-private/model.js:1332
- name
- String
- the name of the relationship
- store
- Store
- returns
- Object
- the inverse relationship, or null
Find the relationship which is the inverse of the one asked for.
For example, if you define models like this:
app/models/post.js | |
1 2 3 |
import Model, { hasMany } from ' -data/model'; export default class PostModel extends Model { |
reload (options) Promise public
Defined in ../packages/model/src/-private/model.js:939
- options
- Object
- optional, may include `adapterOptions` hash which will be passed to adapter request
- returns
- Promise
- a promise that will be resolved with the record when the adapter returns successfully or rejected if the adapter returns with an error.
Reload the record from the adapter.
This will only work if the record has already finished loading.
Example
app/controllers/model/view.js | |
1 2 3 4 |
import Controller from ' /controller'; import { action } from ' /object'; export default class ViewController extends Controller { |
rollbackAttributes public
Defined in ../packages/model/src/-private/model.js:834
Available since v1.13.0
If the model hasDirtyAttributes
this function will discard any unsaved
changes. If the model isNew
it will be removed from the store.
Example
1 2 3 4 5 |
record.name; // 'Untitled Document' record.set('name', 'Doc 1'); record.name; // 'Doc 1' record.rollbackAttributes(); record.name; // 'Untitled Document' |
save (options) Promise public
Defined in ../packages/model/src/-private/model.js:882
- options
- Object
- returns
- Promise
- a promise that will be resolved when the adapter returns successfully or rejected if the adapter returns with an error.
Save the record and persist any changes to the record to an external source via the adapter.
Example
1 2 3 4 5 6 |
record.set('name', 'Tomster'); record.save().then(function() { // Success callback }, function() { // Error callback }); |
If you pass an object using the adapterOptions
property of the options
argument it will be passed to your adapter via the snapshot.
1 |
record.save({ adapterOptions: { subscribe: false } }); |
app/adapters/post.js | |
1 2 3 4 5 6 7 8 9 10 |
import MyCustomAdapter from './custom-adapter'; export default class PostAdapter extends MyCustomAdapter { updateRecord(store, type, snapshot) { if (snapshot.adapterOptions.subscribe) { // ... } // ... } } |
serialize (options) Object public
Defined in ../packages/model/src/-private/model.js:619
- options
- Object
- returns
- Object
- an object whose values are primitive JSON values only
Create a JSON representation of the record, using the serialization strategy of the store's adapter.
serialize
takes an optional hash as a parameter, currently
supported options are:
includeId
:true
if the record's ID should be included in the JSON representation.
toString public
Defined in ../packages/model/src/-private/model.js:2363
Returns the name of the model class.
typeForRelationship (name, store) Model public
Defined in ../packages/model/src/-private/model.js:1266
- name
- String
- the name of the relationship
- store
- Store
- an instance of Store
- returns
- Model
- the type of the relationship, or undefined
For a given relationship name, returns the model type of the relationship.
For example, if you define a model like this:
app/models/post.js | |
1 2 3 |
import Model, { hasMany } from ' -data/model'; export default class PostModel extends Model { |
unloadRecord public
Defined in ../packages/model/src/-private/model.js:753
Unloads the record from the store. This will not send a delete request to your server, it just unloads the record from memory.