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?

wes
  • 7,795
  • 6
  • 31
  • 41
Drew
  • 1,687
  • 5
  • 25
  • 46

8 Answers8

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
    }
}
MarthyM
  • 1,839
  • 2
  • 21
  • 23
b3labs
  • 960
  • 1
  • 14
  • 29
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.

jstricker
  • 2,132
  • 1
  • 30
  • 44
5

You can use something like the code below:

if(panel.getStore().getModifiedRecords().length > 0) {
  console.log('store has dirty records');
}
rrauenza
  • 6,285
  • 4
  • 32
  • 57
ssamuel68
  • 932
  • 13
  • 10
2

I use this:

isStoreModified: function(store)
{
  var modifiedRecords = store.getModifiedRecords();
  return modifiedRecords && modifiedRecords.length  && modifiedRecords.length > 0;
},
boggy
  • 3,674
  • 3
  • 33
  • 56
  • TypeError: Object [object Object] has no method 'getModifiedRecords' – axel wolf Sep 10 '13 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: http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.AbstractStore-method-getModifiedRecords – boggy Sep 12 '13 at 19:53
2
if (panel.getStore().needsSync) {
    //store have unsaved changes.
}
user840250
  • 727
  • 1
  • 8
  • 20
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.

Chris
  • 175
  • 1
  • 1
  • 12
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;
}
computercarguy
  • 2,173
  • 1
  • 13
  • 27
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.

Community
  • 1
  • 1
greghume
  • 11
  • 1