Search code examples
ember.jsember-data

Manually normalizing JSON


I'm using Ember-Data's DS.ActiveModelSerializer. There's an instance where I'm getting data outside of the usual Ember channels, but it still needs to be normalized. I've been attempting to hack together a solution with code like:

DS.ActiveModelSerializer.normalize('model', response.data)

But that results in the error:

DS.ActiveModelSerializer.normalize is not a function

Is it even possible to call a serializer outside of an adapter? If so, how is it managed?


Solution

  • normalize is not a class method of the serializer, so you can't call it via DS.ActiveModelSerializer.

    You can access the serializer from elsewhere though if you have access to the store (which you do in route and controller objects by default, using this.store).

    The store has a serializerFor method, which takes a single argument which is the name of the model. For example, you could do this:

    var serializer = this.store.serializerFor('model');

    You can then use serializer.normalize(...) as you wish.