Working with a Singleton Resource for JSONAPI in Ember with Ember-Data jsonapi adapter

Following up in the last episode of adventures in JSONAPI where I figured out how to get the awesome Jsonapi Resources gem to ‘make’ a singleton resource api – profile, in this case – the next question was to figure out how to interface with it from my Ember JS front end client.

After much gnashing of teeth I found a relatively simple solution : creating a custom adapter for that model which looked like my default application.js adapter but which overrode the buildURL method like so :

1
2
3
4
buildURL: function(record, suffix) {
  var url = this._super()+'/profile';
  return url;
}

then when I need to call my model via GET /profile api I could

1
2
3
4
5
model() {
  return this.store.findAll('profile').then((profile) =>{
  return profile.get('firstObject');
});
}

and the

1
record.save();

would call the PATCH /profile api as expected.

So that was much easier than initially expected. However that being said, I expect to be moving away from a singleton resource call becasue, as stated in the previous post : singletons are not supported by the json-api spec, you probably should not use them. If you have an older version of jsonapi you might already be using a singleton resource, but do not do so in another new api’s you might create.

Leave a Reply