Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store.loadRecords - Only save real changes to datahashes #20

Open
DominikGuzei opened this issue Nov 17, 2011 · 0 comments
Open

Store.loadRecords - Only save real changes to datahashes #20

DominikGuzei opened this issue Nov 17, 2011 · 0 comments

Comments

@DominikGuzei
Copy link

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);
        }
    }
 }

});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant