Last active
December 18, 2015 22:59
-
-
Save bodokaiser/5858197 to your computer and use it in GitHub Desktop.
Native REST Adapter for EmberData
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
(function(App) { | |
App.Adapter = DS.RESTAdapter.extend({ | |
namespace: null, | |
serializer: DS.RESTSerializer.extend({ | |
primaryKey: function() { | |
return '_id'; | |
}, | |
keyForAttributeName: function(type, name) { | |
return name; | |
}, | |
extract: function(loader, json, type, record) { | |
var root = this.pluralize(this.rootForType(type)); | |
this.sideload(loader, type, json, root); | |
this.extractMeta(loader, type, json); | |
if (!json) Ember.Logger.warn('JSON response was empty.'); | |
if (record) loader.updateId(record, json); | |
this.extractRepresentation(loader, type, json); | |
}, | |
extractMany: function(loader, json, type, records) { | |
var root = this.pluralize(this.rootForType(type)); | |
if (records) records = records.toArray(); | |
if (!json) Ember.Logger.warn('JSON response was empty.'); | |
var refs = []; | |
for (var i = 0; i < json.length; i++) { | |
if (records) loader.updateId(records[i], json[i]); | |
refs.push(this.extractRecordRepresentation(loader, type, json[i])); | |
} | |
loader.populateArray(refs); | |
} | |
}), | |
createRecord: function(store, type, record) { | |
var root = this.rootForType(type); | |
var adapter = this; | |
return this.ajax(this.buildURL(root), "POST", { | |
data: this.serialize(record, { includeId: true }) | |
}).then(function(json){ | |
adapter.didCreateRecord(store, type, record, json); | |
}, function(xhr) { | |
adapter.didError(store, type, record, xhr); | |
throw xhr; | |
}).then(null, DS.rejectionHandler); | |
}, | |
createRecords: function(store, type, records) { | |
var adapter = this; | |
if (Ember.get(this, 'bulkCommit') === false) { | |
return this._super(store, type, records); | |
} | |
var root = this.rootForType(type); | |
var plural = this.pluralize(root); | |
var data = []; | |
records.forEach(function(record) { | |
data.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); | |
}, | |
updateRecord: function(store, type, record) { | |
var id = Ember.get(record, 'id'); | |
var root = this.rootForType(type); | |
var adapter = this; | |
return this.ajax(this.buildURL(root, id), "PUT",{ | |
data: this.serialize(record) | |
}).then(function(json){ | |
adapter.didUpdateRecord(store, type, record, json); | |
}, function(xhr) { | |
adapter.didError(store, type, record, xhr); | |
throw xhr; | |
}).then(null, DS.rejectionHandler); | |
}, | |
updateRecords: function(store, type, records) { | |
if (Ember.get(this, 'bulkCommit') === false) { | |
return this._super(store, type, records); | |
} | |
var root = this.rootForType(type); | |
var plural = this.pluralize(root); | |
var adapter = this; | |
var data = []; | |
records.forEach(function(record) { | |
data.push(this.serialize(record, { includeId: true })); | |
}, this); | |
return this.ajax(this.buildURL(root, "bulk"), "PUT", { | |
data: data | |
}).then(function(json) { | |
adapter.didUpdateRecords(store, type, records, json); | |
}).then(null, DS.rejectionHandler); | |
}, | |
deleteRecord: function(store, type, record) { | |
var id = Ember.get(record, 'id'); | |
var root = this.rootForType(type); | |
var adapter = this; | |
return this.ajax(this.buildURL(root, id), "DELETE").then(function(json){ | |
adapter.didDeleteRecord(store, type, record, json); | |
}, function(xhr){ | |
adapter.didError(store, type, record, xhr); | |
throw xhr; | |
}).then(null, DS.rejectionHandler); | |
}, | |
deleteRecords: function(store, type, records) { | |
if (Ember.get(this, 'bulkCommit') === false) { | |
return this._super(store, type, records); | |
} | |
var root = this.rootForType(type); | |
var plural = this.pluralize(root); | |
var serializer = Ember.get(this, 'serializer'); | |
var adapter = this; | |
var data = []; | |
records.forEach(function(record) { | |
data.push(serializer.serializeId( get(record, 'id') )); | |
}); | |
return this.ajax(this.buildURL(root, 'bulk'), "DELETE", { | |
data: data | |
}).then(function(json){ | |
adapter.didDeleteRecords(store, type, records, json); | |
}).then(null, DS.rejectionHandler); | |
} | |
}); | |
})(App); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment