Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mspisars/6053682 to your computer and use it in GitHub Desktop.
Save mspisars/6053682 to your computer and use it in GitHub Desktop.
createRecords: function(store, type, records) {
var adapter = this, serializer = this.serializer, initialRecordsToCreate = [];
if (get(this, 'bulkCommit') === false) {
records.forEach(function(record) {
var deferSave = false
record.eachRelationship(function(name, relationship) {
var key = serializer._keyForBelongsTo(record.constructor, name),
child, createDependentRecords;
if (relationship.kind === 'belongsTo') {
if (!serializer.embeddedType(type, name)) {
child = get(record, relationship.key);
createDependentRecords = function() {
adapter.createRecords(store, type, [record]);
child.removeObserver('id', createDependentRecords);
};
if(child && !get(child, 'id')) {
child.addObserver('id', adapter, createDependentRecords);
deferSave = true;
}
}
}
}, this);
if(!deferSave) {
initialRecordsToCreate.push(record);
}
});
return this._super(store, type, initialRecordsToCreate);
}
var root = this.rootForType(type),
plural = this.pluralize(root);
var data = {};
data[plural] = [];
records.forEach(function(record) {
data[plural].push(this.serialize(record, { includeId: true }));
}, this);
return this.ajax(this.buildURL(root), "POST", {
data: data
}).then(function(json) {
adapter.didCreateRecords(store, type, records, json);
}).then(null, DS.rejectionHandler);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment