Created
March 23, 2015 01:40
-
-
Save danshultz/68190dbf76c2d775e7f8 to your computer and use it in GitHub Desktop.
Ember Data Patch Support
This file contains 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
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 }); | |
} | |
}); |
This file contains 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
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); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you been using this lately? With ember-data 1.0.0-beta.17? Seems straight forward and something I'd like to try out. Thanks