Skip to content

Instantly share code, notes, and snippets.

@danshultz
Created March 23, 2015 01:40
Show Gist options
  • Save danshultz/68190dbf76c2d775e7f8 to your computer and use it in GitHub Desktop.
Save danshultz/68190dbf76c2d775e7f8 to your computer and use it in GitHub Desktop.
Ember Data Patch Support
import Ember from 'ember';
var get = Ember.get;
export default Ember.Mixin.create({
updateRecord: function (store, type, record) {
var data = {};
var serializer = store.serializerFor(type.typeKey);
serializer.serializeIntoHash(data, type, record, {
includeId: true,
onlyDirtyAttributes: true
});
var id = get(record, 'id');
var url = this.buildURL(type.typeKey, id, record);
return this.ajax(url, "PATCH", { data: data });
}
});
import Ember from 'ember';
var get = Ember.get;
export default Ember.Mixin.create({
serialize: function(record, options) {
var json = {};
if (options) {
if (options.includeId) {
let id = record.id;
if (id) {
json[get(this, 'primaryKey')] = id;
}
}
if (options.onlyDirtyAttributes) {
let dirtyAttributes = get(record, '_inFlightAttributes');
record.eachAttribute(function(key, attribute) {
if (key in dirtyAttributes) {
this.serializeAttribute(record, json, key, attribute);
}
}, this);
return json;
}
}
return this._super(record, options);
}
});
@kmiyashiro
Copy link

I'd like to use with 2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment