0
votes

I've got an Ember CLI app using a Ruby on Rails API backend. I initially built this using a REST API, and am trying to migrate it to use JSONAPI.

To send data from Rails to Ember this is simple - I have just updated the ActiveModelSerializers gem that I was already using, and have updated the adapter and serialiser in the Ember app to be something like:

export default DS.JSONAPIAdapter;

export default DS.JSONAPISerializer;

The problem is that it is a lot of work to rewrite my whole Rails app to respond to a new data format, so I would like to stick with the REST adapter/serializer when sending data from Ember to Rails (at least in the short term). I am not sure how to do this. I was thinking of something like this:

export default DS.JSONAPISerializer.extend({
  serialize(snapshot, options){
    return DS.RESTSerializer.serialize(snapshot, options);
  }
});

This doesn't work. Has anyone done this? Is there a good way to do it?

Many thanks

1
DS.RESTSerializer is a class, you would need to instantiate it with .create(). - Lux
Thanks @Lux. Still doesn't work though - I'm now getting the error Uncaught TypeError: Cannot read property 'lookup' of undefined - AdamP
Maybe you should look it up from the DI container? - Lux
Any chance of some example code? I'm not quite sure how to do that, am a bit out of my depth with this problem! Thanks - AdamP

1 Answers

0
votes

Basically DS.RESTSerializer is a class, but serialize is an instance method. So you have to .create() the serializer:

let instance = DS.RESTSerializer.create().serialize(...arguments)

However this will not work because the Serializer does access the DI container, and so expects to be initialized by the container. You can manually inject the owner like this:

DS.RESTSerializer.create(Ember.getOwner(this).ownerInjection()).serialize(...arguments);