DS.Adapter#createRecord()

createRecord (store, type, snapshot) Promise

Defined in addon/adapter.js:282

Implement this method in a subclass to handle the creation of new records.

Serializes the record and sends it to the server.

Example

app/adapters/application.js
import DS from 'ember-data';

export default DS.Adapter.extend({
  createRecord: function(store, type, snapshot) {
    var data = this.serialize(snapshot, { includeId: true });

    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.$.ajax({
        type: 'POST',
        url: `/${type.modelName}`,
        dataType: 'json',
        data: data
      }).then(function(data) {
        Ember.run(null, resolve, data);
      }, function(jqXHR) {
        jqXHR.then = null; // tame jQuery's ill mannered promises
        Ember.run(null, reject, jqXHR);
      });
    });
  }
});

Parameters:

store DS.Store
type DS.Model
the DS.Model class of the record
snapshot DS.Snapshot

Returns:

Promise
promise
doc_EmberJs
2016-11-30 16:49:22
Comments
Leave a Comment

Please login to continue.