Created
October 13, 2016 23:49
-
-
Save SaladFork/812192cd15863abc3a7161276f9e7d76 to your computer and use it in GitHub Desktop.
Solution for Nested API Resources
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
/* | |
Here's a technique I've used for telling my adapter to deal with a nested API | |
resource when there is an ember-data relationship set up from the child to the | |
parent. The below *does* use the private `_buildURL` and is specific to | |
RESTAdapter, but hopefully it might be helpful to you? | |
*/ | |
import DS from 'ember-data'; | |
const { | |
RESTAdapter | |
} = DS; | |
export default RESTAdapter.extend({ | |
// Example for overriding a multi-response endpoint | |
urlForFindAll(modelName, snapshot) { | |
let baconId = snapshot.belongsTo('bacon').id; | |
// NOTE: `_buildURL` is private! | |
let baconEndpoint = this._buildURL('bacon', baconId); | |
return `${baconEndpoint}/aiolis`; | |
}, | |
// Example for overriding a single-response endpoint | |
urlForFindRecord(id, modelName, snapshot) { | |
let baconId = snapshot.belongsTo('bacon').id; | |
// NOTE: `_buildURL` is private! | |
let baconEndpoint = this._buildURL('bacon', baconId); | |
return `${baconEndpoint}/aiolis/${id}`; | |
} | |
// See more hooks at: | |
// http://emberjs.com/api/data/classes/DS.RESTAdapter.html#index | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment