Created
October 21, 2015 07:22
-
-
Save ksol/c5730bfe50094adca56b to your computer and use it in GitHub Desktop.
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 ApplicationAdapter from './application'; | |
import UrlTemplates from "ember-data-url-templates"; | |
export default ApplicationAdapter.extend(UrlTemplates, { | |
updateRecordUrlTemplate: '{+host}/posts/{id}{/updateEndpoint}', | |
urlSegments: { | |
updateEndpoint(type, id, snapshot) { | |
return snapshot.get('_updateEndpoint'); | |
} | |
}, | |
// Overriden method from RESTAdapter@v1.13.13 | |
// Do not forget to update with every ED upgrade | |
updateRecord: function(store, type, snapshot) { | |
var data = {}; | |
var serializer = store.serializerFor(type.modelName); | |
serializer.serializeIntoHash(data, type, snapshot); | |
var id = snapshot.id; | |
var url = this.buildURL(type.modelName, id, snapshot, 'updateRecord'); | |
var verb = snapshot.get('_updateVerb') || "PUT"; | |
return this.ajax(url, verb, { 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"; | |
import DS from "ember-data"; | |
export default DS.Model.extend({ | |
// Your ED relationships and properties here... | |
// ... | |
// Custom API | |
upvote() { | |
this.setProperties({ | |
_updateEndpoint: 'upvote', | |
_updateVerb: 'POST' | |
}); | |
let promise = this.save(); | |
promise.finally(() => { | |
this.setProperties({ | |
_updateEndpoint: null, | |
_updateVerb: null | |
}); | |
}); | |
return promise; | |
}, | |
downvote() { | |
this.setProperties({ | |
_updateEndpoint: 'downvote', | |
_updateVerb: 'POST' | |
}); | |
let promise = this.save(); | |
promise.finally(() => { | |
this.setProperties({ | |
_updateEndpoint: null, | |
_updateVerb: null | |
}); | |
}); | |
return promise; | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment