Class DS.Model

The model class that all Ember Data records descend from. This is the public API of Ember Data models. If you are using Ember Data in your application, this is the class you should use. If you are working on Ember Data internals, you most likely want to be dealing with InternalModel

Show:

Module: ember-data
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.

Example

app/models/mascot.js
1
2
3
4
5
import DS from 'ember-data';

export default DS.Model.extend({
  name: attr('string')
});
1
2
3
4
var mascot = store.createRecord('mascot');
mascot.changedAttributes(); // {}
mascot.set('name', 'Tomster');
mascot.changedAttributes(); // {name: [undefined, 'Tomster']}
Module: ember-data

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 it was made.

Example

app/routes/model/delete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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();
    }
  }
});
Module: ember-data
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/routes/model/delete.js
1
2
3
4
5
6
7
8
9
10
11
12
import Ember from 'ember';

export default Ember.Route.extend({
  actions: {
    delete: function() {
      var controller = this.controller;
      controller.get('model').destroyRecord().then(function() {
        controller.transitionToRoute('model.index');
      });
    }
  }
});
Module: ember-data
proto
Object
key
String
value
Ember.ComputedProperty

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:

1
2
3
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.

Module: ember-data
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 iteration
  • meta 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
4
5
6
7
8
9
10
11
12
13
14
15
16
import DS from 'ember-data';

var Person = DS.Model.extend({
  firstName: attr('string'),
  lastName: attr('string'),
  birthday: 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"}
Module: ember-data
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.

Module: ember-data
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.

Module: ember-data
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 iteration
  • type 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 DS from 'ember-data';

var Person = DS.Model.extend({
  firstName: attr(),
  lastName: attr('string'),
  birthday: attr('date')
});

Person.eachTransformedAttribute(function(name, type) {
  console.log(name, type);
});

// prints:
// lastName string
// birthday date
Module: ember-data
name
String
the name of the relationship
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
4
5
import DS from 'ember-data';

export default DS.Model.extend({
  comments: DS.hasMany('message')
});
app/models/message.js
1
2
3
4
5
import DS from 'ember-data';

export default DS.Model.extend({
  owner: DS.belongsTo('post')
});

App.Post.inverseFor('comments') -> { type: App.Message, name: 'owner', kind: 'belongsTo' } App.Message.inverseFor('owner') -> { type: App.Post, name: 'comments', kind: 'hasMany' }

Module: ember-data
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 and has not yet been modified (isLoaded but not isDirty, or isSaving).

Example

app/routes/model/view.js
1
2
3
4
5
6
7
8
9
10
11
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
      });
    }
  }
});
Module: ember-data

If the model isDirty 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.get('name'); // 'Untitled Document'
record.set('name', 'Doc 1');
record.get('name'); // 'Doc 1'
record.rollbackAttributes();
record.get('name'); // 'Untitled Document'
Module: ember-data
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
});
Module: ember-data
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.
Module: ember-data
options
Object
returns
Object
A JSON representation of the object.

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.
Module: ember-data
name
String
the name of the relationship
store
Store
an instance of DS.Store
returns
DS.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
4
5
import DS from 'ember-data';

export default DS.Model.extend({
  comments: DS.hasMany('comment')
});

Calling App.Post.typeForRelationship('comments') will return App.Comment.