Search code examples
serializationember.jsember-data

ember data: customize serializer used by toJSON


In my ember-data-using application I have an application serializer (mainly to take care of polymorphic relationships properly). However, its not used when I call toJSON() on a model instance, its not used -- rather the default JSONSerializer is used instead.

How can I customize the serializer used by toJSON()?


Solution

  • First of all i hope i understood properly your question...

    Well, If you want to get the same result as in your ApplicationSerializer just call this.serialize() in your model.

    If you want absolutely use the toJSON method without overwriting the default DS.Model of your whole app you can still make something like that :

    App.CustomModel = DS.Model.extend({
      toJSON: function(){
        return this.serialize();
      }  
    });
    

    and then make all the concerned model extend this CustomModel

    App.Color = App.CustomModel.extend({
      name : DS.attr("string")
    });
    

    here is a working jsbin