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

8

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?

Pamalapamela answered 22/12, 2011 at 2:30 Comment(0)
D
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
    }
}
Decerebrate answered 22/12, 2011 at 12:32 Comment(0)
E
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.

Eulogize answered 24/1, 2014 at 19:10 Comment(0)
P
5

You can use something like the code below:

if(panel.getStore().getModifiedRecords().length > 0) {
  console.log('store has dirty records');
}
Price answered 20/12, 2013 at 18:48 Comment(0)
F
2

I use this:

isStoreModified: function(store)
{
  var modifiedRecords = store.getModifiedRecords();
  return modifiedRecords && modifiedRecords.length  && modifiedRecords.length > 0;
},
Fidelafidelas answered 23/5, 2013 at 20:6 Comment(2)
TypeError: Object [object Object] has no method 'getModifiedRecords'Battleax
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/…Fidelafidelas
T
2
if (panel.getStore().needsSync) {
    //store have unsaved changes.
}
Tallent answered 21/2, 2015 at 10:16 Comment(1)
This is not a public property in the ExtJS API.Rapeseed
P
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.

Placeman answered 7/4, 2015 at 21:55 Comment(0)
D
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;
}
Drayage answered 17/10, 2013 at 14:37 Comment(0)
I
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.

Itemize answered 25/3, 2015 at 0:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.