sproutcore/sproutcore-datastore — Issue #20: Store.loadRecords - Only save real changes to datahashes
Author: DominikGuzei
Created: 2011-11-17 14:55:36+00:00
State: open

Hey Sproutcore Team!

I started building my first real project with Sproutcore 2 and I am amazed how nice all the parts fit together!
Still I want to share one thing that I didn't find logical or maybe I would even call it a bug:

For my app I periodically fetch records of different types from the backend to see if properties changed on them. When they arrive at the data source I push the records into the store with loadRecords as suggested. The only problem is that althought most of the records would stay the same (data hashes have same data as before) the loadRecords method invokes dataHashDidChange for each of them. This results in refreshing the complete view layer for each fetch and made it impossible to stay at specific scroll position because since for very short time the (in my case pretty complex) tree was gone and rebuilt.

Even if it wasn't that obvious like in my case -> a short lookup if actually anything changed on the data hash would be far better!?

Please tell me if I totally messed up some concepts, but here is the hacky solution I used for now in my app:

var PatchedStore = SC.Store.extend({

/**
* @Overrides SC.Store.loadRecord
*
* Since original loadRecord method doesnt check if
* the datahash actually changed this is a monkey patch
* to introduce this to the store class. Otherwise all
* bindings would fire and the whole view be rebuild
* alhought nothing changed on the record.
*/
loadRecord: function(recordType, loadedHash, id) {
if(loadedHash || id) {
var key = this.storeKeyFor(recordType, loadedHash.id ? loadedHash.id : id);
var existingHash = this.readEditableDataHash(key);

// not new record -> compare contents
if(!existingHash || JSON.stringify(loadedHash) != JSON.stringify(existingHash)) {
this._super(recordType, loadedHash);
}
}
}

});