Skip to content

DS.Store.load should not overwrite the old information #462

Closed
@ftokarev

Description

@ftokarev

Usecase: server pushes updates to already loaded records. In order to save bandwidth, the server pushes only changed attributes and not the whole record. As a result, we end up with the record with a lot of missing attributes.

As a work-around, I've implemented this:

App.Store = DS.Store.extend({

  load: function (type, id, hash) {
    var id = id+'',
        typeMap = this.typeMapFor(type),
        clientId = typeMap.idToCid[id],
        currentHash, resultHash;

    if (clientId) {
      var record = this.recordCache[clientId];
      if (record) {
        // record is materialized and data is processed
        currentHash = this.getDataOfMaterializedRecord(record);
      } else {
        // record is not materialized yet, its hash is cached
        currentHash = this.clientIdToHash[clientId];
      }
      resultHash = $.extend({}, currentHash, hash);
    } else {
      resultHash = hash;
    }

    this._super(type, id, resultHash);
  },

  getDataOfMaterializedRecord: function (record) {
    var data = record.get("data");
    return $.extend({}, data.attributes, data.belongsTo, data.hasMany);
  },
});

Metadata

Metadata

Assignees

Labels

🏷️ featThis PR introduces a new feature

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions