Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a store (or records) have been edited?

Tags:

extjs

extjs4

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?

like image 920
Drew Avatar asked Dec 22 '11 02:12

Drew


3 Answers

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.

like image 136
jstricker Avatar answered Nov 15 '22 22:11

jstricker


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
    }
}
like image 20
b3labs Avatar answered Nov 15 '22 22:11

b3labs


You can use something like the code below:

if(panel.getStore().getModifiedRecords().length > 0) {
  console.log('store has dirty records');
}
like image 5
ssamuel68 Avatar answered Nov 15 '22 22:11

ssamuel68