Forked from robmonie/ember data dependent createRecords
Created
July 22, 2013 13:06
-
-
Save mspisars/6053682 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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