Nefarious
Nefarious

Reputation: 995

How to force ember data to reset all fields of a record on reload?

I am using ember with ember data and i have a scenario where I need to reload a model. On reload, when the data is fetched and some fields of that model are null, the older data still persists. For eg., if I have a Post model

App.Post = DS.Model.extend({
  name: DS.attr('string'),
  description: DS.attr('string'),
});

Now the first time the server returns the following data:

{
  "post" : {
              "id" : "1",
              "name" : "new post",
              "description": "some description"
           }
}

After reload is called, the server returns the following data:

{
  "post" : {
              "id" : "1",
              "name" : "new post",
           }
}

So after the reload, "description" field should be set to null for that record. But the old data ie., "some description" still persists in that field.

How can I force ember data to reset all fields on reload?

Upvotes: 1

Views: 273

Answers (1)

GJK
GJK

Reputation: 37369

Unfortunately the exact feature you want was deprecated a few months ago. At the bottom it mentions a way in the serializer to replace missing properties with null.

// app/serializers/application.js
// or App.ApplicationSerializer
export default DS.RESTSerializer.extend({
  normalize: function(type, hash, prop) {
    hash = this._super(type, hash, prop);

    // Find missing attributes and replace them with `null`
    type.eachAttribute(function(key) {
      if (!hash.hasOwnProperty(key)) {
        hash[key] = null;
      }
    });

    return hash;
  }
});

Upvotes: 2

Related Questions