9

I have a Grid Panel, which when I leave the page, I want a check to see if any items in the store (or iterate through models/records) to check if there are any unsaved changes/additions.

I initially tried using panel.getStore().getNewRecords() for new records, but it returns every record currently paged in. panel.getStore().getUpdatedRecords() seems to ignore records, despite lines in the grid having the small red triangle in each cell.

So can anyone advise on the correct way to check if any new or updated records exist in a store?

8 Answers 8

20

This may work for you.

var records = store.getRange();

for (var i = 0; i < records.length; i++) {
    var rec = records[i];

    if (rec.dirty == true) {
        //Save data
    }
}
13

In order to keep the dirty checking logic encapsulated, I chose to add an isDirty() method to the Ext.data.Store object. I utilized the same logic that AbstractStore.sync() uses to determine whether the store needs to sync.

Ext.define(null, {
    override: "Ext.data.Store",
    isDirty: function() {
        return (this.getNewRecords().length > 0 || this.getUpdatedRecords().length > 0 || this.getRemovedRecords().length > 0);
    }
});

I'm using ExtJS 4.2.1. If all of your records are returned when you call getNewRecords() you should check that you've set a value for idProperty on your model.

0
5

You can use something like the code below:

if(panel.getStore().getModifiedRecords().length > 0) {
  console.log('store has dirty records');
}
0
2

I use this:

isStoreModified: function(store)
{
  var modifiedRecords = store.getModifiedRecords();
  return modifiedRecords && modifiedRecords.length  && modifiedRecords.length > 0;
},
2
  • TypeError: Object [object Object] has no method 'getModifiedRecords'
    – axel wolf
    Sep 10, 2013 at 10:52
  • What version of Ext Js do you use? The function is in AbstractStore and I double-checked by searching the Ext Js 4.1.3 and 4.2.1 source code. See this: docs.sencha.com/extjs/4.2.1/#!/api/…
    – boggy
    Sep 12, 2013 at 19:53
2
if (panel.getStore().needsSync) {
    //store have unsaved changes.
}
1
  • This is not a public property in the ExtJS API. Aug 8, 2019 at 14:50
1

If you are new to overriding here is @jstrickers answer for a mvc style program:

Create:

    Ext.define('YourFolder.overridefolder.DataStore', { 

    override: "Ext.data.Store",
    isDirty: function() {
        return (this.getNewRecords().length > 0 || this.getUpdatedRecords().length > 0 || this.getRemovedRecords().length > 0);
    }

});

Add this file to the appropriate folder and point to it in app.js 'required', then the isDirty will be available in your ext.data.store

Works great and the function will be available were you need it.

0

I based this off the answer by @theboulderer. I know this is an old thread, but I thought it might be useful for others who find this page.

I'm using the MVC model, so keep that in mind.

All you have to do is pass the store to the function, and it returns a boolean.

Usage:

if (this.isDirtyStore(myStore)){
    ...
}

Function:

isDirtyStore: function(theStore){
    var isDirty = false;

    theStore.each(function(item){
        if(item.dirty == true){
            isDirty = true;
        }
    });
    if (!isDirty){
        isDirty = (theStore.removed.length > 0);
    }
    return isDirty;
}
0

The answer by shw in this thread How do I get the dirty records from an ExtJS data store?, answers your question.

The trick is that the store reader needs to designate a idProperty or all rows are considered new.

They provide an example as well.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.