Extends: | Ember.Object |
---|---|
Uses: | Ember.Evented |
Defined in: | addon/-private/system/model/model.js:70 |
Module: | ember-data |
Defined in addon/-private/system/model/model.js:849
Get the reference for the specified belongsTo relationship.
Example
export default DS.Model.extend({ user: DS.belongsTo({ async: true }) });
let blog = store.push({ data: { type: 'blog', id: 1, relationships: { user: { data: { type: 'user', id: 1 } } } } }); let userRef = blog.belongsTo('user'); // check if the user relationship is loaded let isLoaded = userRef.value() !== null; // get the record of the reference (null if not yet available) let user = userRef.value(); // get the identifier of the reference if (userRef.remoteType() === "id") { let id = userRef.id(); } else if (userRef.remoteType() === "link") { let link = userRef.link(); } // load user (via store.findRecord or store.findBelongsTo) userRef.load().then(...) // or trigger a reload userRef.reload().then(...) // provide data for reference userRef.push({ type: 'user', id: 1, attributes: { username: "@user" } }).then(function(user) { userRef.value() === user; });
Defined in addon/-private/system/model/model.js:643
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
import DS from 'ember-data'; export default DS.Model.extend({ name: DS.attr('string'), isAdmin: DS.attr('boolean', { defaultValue: false }) });
let mascot = store.createRecord('mascot'); mascot.changedAttributes(); // {} mascot.set('name', 'Tomster'); mascot.changedAttributes(); // { name: [undefined, 'Tomster'] } mascot.set('isAdmin', true); mascot.changedAttributes(); // { isAdmin: [undefined, true], name: [undefined, 'Tomster'] } mascot.save().then(function() { mascot.changedAttributes(); // {} mascot.set('isAdmin', false); mascot.changedAttributes(); // { isAdmin: [true, false] } });
Defined in addon/-private/system/model/model.js:537
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
import Ember from 'ember'; export default Ember.Route.extend({ actions: { softDelete: function() { this.controller.get('model').deleteRecord(); }, confirm: function() { this.controller.get('model').save(); }, undo: function() { this.controller.get('model').rollbackAttributes(); } } });
Defined in addon/-private/system/model/model.js:569
Same as deleteRecord
, but saves the record immediately.
Example
import Ember from 'ember'; export default Ember.Route.extend({ actions: { delete: function() { let controller = this.controller; controller.get('model').destroyRecord().then(function() { controller.transitionToRoute('model.index'); }); } } });
If you pass an object on the adapterOptions
property of the options argument it will be passed to your adapter via the snapshot
record.destroyRecord({ adapterOptions: { subscribe: false } });
import MyCustomAdapter from './custom-adapter'; export default MyCustomAdapter.extend({ deleteRecord: function(store, type, snapshot) { if (snapshot.adapterOptions.subscribe) { // ... } // ... } });
Defined in addon/-private/system/model/model.js:1888
This Ember.js hook allows an object to be notified when a property is defined.
In this case, we use it to be notified when an Ember Data user defines a belongs-to relationship. In that case, we need to set up observers for each one, allowing us to track relationship changes and automatically reflect changes in the inverse has-many array.
This hook passes the class being set up, as well as the key and value being defined. So, for example, when the user does this:
DS.Model.extend({ parent: DS.belongsTo('user') });
This hook would be called with "parent" as the key and the computed property returned by DS.belongsTo
as the value.
Defined in addon/-private/system/model/model.js:1734
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):
function(name, meta);
name
the name of the current property in the iterationmeta
the meta object for the attribute property in the iterationNote that in addition to a callback, you can also pass an optional target object that will be set as this
on the context.
Example
import DS from 'ember-data'; let Person = DS.Model.extend({ firstName: DS.attr('string'), lastName: DS.attr('string'), birthday: DS.attr('date') }); Person.eachAttribute(function(name, meta) { console.log(name, meta); }); // prints: // firstName {type: "string", isAttribute: true, options: Object, parentType: function, name: "firstName"} // lastName {type: "string", isAttribute: true, options: Object, parentType: function, name: "lastName"} // birthday {type: "date", isAttribute: true, options: Object, parentType: function, name: "birthday"}
Defined in addon/-private/system/model/model.js:1590
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.
Defined in addon/-private/system/model/model.js:1044
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):
function(name, descriptor);
name
the name of the current property in the iterationdescriptor
the meta object that describes this relationshipThe relationship descriptor argument is an object with the following properties.
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
import DS from 'ember-data'; export default DS.JSONSerializer.extend({ serialize: function(record, options) { let json = {}; record.eachRelationship(function(name, descriptor) { if (descriptor.kind === 'hasMany') { let serializedHasManyName = name.toUpperCase() + '_IDS'; json[serializedHasManyName] = record.get(name).mapBy('id'); } }); return json; } });
Defined in addon/-private/system/model/model.js:1783
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):
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 attributeNote that in addition to a callback, you can also pass an optional target object that will be set as this
on the context.
Example
import DS from 'ember-data'; let Person = DS.Model.extend({ firstName: DS.attr(), lastName: DS.attr('string'), birthday: DS.attr('date') }); Person.eachTransformedAttribute(function(name, type) { console.log(name, type); }); // prints: // lastName string // birthday date
Defined in addon/-private/system/model/model.js:914
Get the reference for the specified hasMany relationship.
Example
// models/blog.js export default DS.Model.extend({ comments: DS.hasMany({ async: true }) }); let blog = store.push({ data: { type: 'blog', id: 1, relationships: { comments: { data: [ { type: 'comment', id: 1 }, { type: 'comment', id: 2 } ] } } } }); let commentsRef = blog.hasMany('comments'); // check if the comments are loaded already let isLoaded = commentsRef.value() !== null; // get the records of the reference (null if not yet available) let comments = commentsRef.value(); // get the identifier of the reference if (commentsRef.remoteType() === "ids") { let ids = commentsRef.ids(); } else if (commentsRef.remoteType() === "link") { let link = commentsRef.link(); } // load comments (via store.findMany or store.findHasMany) commentsRef.load().then(...) // or trigger a reload commentsRef.reload().then(...) // provide data for reference commentsRef.push([{ type: 'comment', id: 1 }, { type: 'comment', id: 2 }]).then(function(comments) { commentsRef.value() === comments; });
Defined in addon/-private/system/model/model.js:1235
Find the relationship which is the inverse of the one asked for.
For example, if you define models like this:
import DS from 'ember-data'; export default DS.Model.extend({ comments: DS.hasMany('message') });
store.modelFor('post').inverseFor('comments', store) // { type: App.Message, name: 'owner', kind: 'belongsTo' } store.modelFor('message').inverseFor('owner', store) // { type: App.Post, name: 'comments', kind: 'hasMany' }
Defined in addon/-private/system/model/model.js:788
Reload the record from the adapter.
This will only work if the record has already finished loading.
Example
import Ember from 'ember'; export default Ember.Route.extend({ actions: { reload: function() { this.controller.get('model').reload().then(function(model) { // do something with the reloaded model }); } } });
Defined in addon/-private/system/model/model.js:1855
Discards any unsaved changes to the given attribute. This feature is not enabled by default. You must enable ds-rollback-attribute
and be running a canary build.
Example
record.get('name'); // 'Untitled Document' record.set('name', 'Doc 1'); record.get('name'); // 'Doc 1' record.rollbackAttribute('name'); record.get('name'); // 'Untitled Document'
Defined in addon/-private/system/model/model.js:709
If the model hasDirtyAttributes
this function will discard any unsaved changes. If the model isNew
it will be removed from the store.
Example
record.get('name'); // 'Untitled Document' record.set('name', 'Doc 1'); record.get('name'); // 'Doc 1' record.rollbackAttributes(); record.get('name'); // 'Untitled Document'
Defined in addon/-private/system/model/model.js:742
Save the record and persist any changes to the record to an external source via the adapter.
Example
record.set('name', 'Tomster'); record.save().then(function() { // Success callback }, function() { // Error callback });
If you pass an object on the adapterOptions
property of the options argument it will be passed to you adapter via the snapshot
record.save({ adapterOptions: { subscribe: false } });
import MyCustomAdapter from './custom-adapter'; export default MyCustomAdapter.extend({ updateRecord: function(store, type, snapshot) { if (snapshot.adapterOptions.subscribe) { // ... } // ... } });
Defined in addon/-private/system/model/model.js:419
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.Defined in addon/-private/system/model/model.js:437
Use DS.JSONSerializer to get the JSON representation of a record.
toJSON
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.Defined in addon/-private/system/model/model.js:1205
For a given relationship name, returns the model type of the relationship.
For example, if you define a model like this:
import DS from 'ember-data'; export default DS.Model.extend({ comments: DS.hasMany('comment') });
Calling store.modelFor('post').typeForRelationship('comments', store)
will return Comment
.
Defined in addon/-private/system/model/model.js:619
Unloads the record from the store. This will cause the record to be destroyed and freed up for garbage collection.
© 2017 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://emberjs.com/api/ember-data/2.14/classes/DS.Model/methods