Search code examples
javascriptember.jsember-data

Ember Data: Persist fields not in model


I am developing an Ember application for an existing API. This API allows any arbitrary fields in the JSON payload, and will save all of them. When defining models in Ember I cannot anticipate all the fields that may be used. Unfortunately, any update to a model means that these fields will be lost. I'm looking for a solution that will persist fields that were retrieved by the API but not defined in the model.

Thanks.


Solution

  • You can define a model with an ember data attribute without a type. Then you would store your objects with arbitrary keys at this attribute.

    export default Model.extend({
      data: attr()
    });
    

    This way, you can set anything at this prop including entire objects, i.e. model.set('data', /* anything */) and it will be stored in ember data.