How to detect if the store of a grid has changed or is dirty?

  1. #1

    How to detect if the store of a grid has changed or is dirty?

    I have a forum which has 4 expandable subtabs and one of them has a grid.In this function below I am checking if the tabs have been modified/changed or are "dirty" to be able to save the main tab.

    hasDirtyChild(this._firstTab);
    hasDirtyChild(this._TabWithGrid);
    hasDirtyChild(this._thirdTab);
    hasDirtyChild(this._FouthTab);
    To add items to the grid, I have seprate window that comes up and saves the item to the grid. However the grid items haven't been saved to the database yet, because the main tab isn't saved yet. So the PK's/ propertyId's of those records are negative right now. So I thought if I check if the store is negative I will then trigger the save function in the backend. It is working, however now, there is an issue where my function is checking all the comboboxes since they all have stores. Is there a way to only check the store of the grids instead of all stores that exist on my forum? I have this function used in a lot of places and I don't want it to cause performance issues by checking all comboboxes stores. I only want to check the stores of grids if they exist.

    	function hasDirtyChild(cnt) {
    
    		if (!cnt)
    			return false;
    		var ret = false;
    		Ext.each(cnt.query('*'), function (item) {
    			if (item.isDirty && item.isDirty()) {
    				ret = true;
    
    			}
    			if (item.store) {
    				for (var i = 0; i < item.store.getCount(); i++) {
    					var record = item.store.getAt(i);
    					if (record.get(record.idProperty)< 0) {
    						ret = true;
    						break;
    					}
    				}
    			}	
    		});
    		return ret;
    	}
    Last edited by fabricio.murta; Apr 02, 2019 at 7:47 PM.
  2. #2
    Hello @juan!

    Just a quick feedback on the posting guidelines, whenever you add code blocks to your posts, please wrap them in [code][/code] tags. I've went ahead and edited your post to change it. You'd notice how it improves, as it allows also indentation to be properly displayed, as well as a nice outline box to copy-paste the code.
    Last edited by fabricio.murta; Apr 03, 2019 at 4:29 PM. Reason: Fixes tag from 'quote' to 'code'.
  3. #3
    Hello again, @juan!

    Now to the question, you may try the undocumented (thus, unsupported) store's isDirty() method. But if you want to rely on documented code, you can just do what the isDirty() does (and perhaps fine tune for your needs -- now that you don't really need it for new records:

    function storeDirty() {
        var store = App.MyStore1;
        return store.getNewRecords().length > 0 || store.getUpdatedRecords().length > 0 || store.getRemovedRecords().length > 0;
    }
    Those methods are all documented, as follows, so they should be supported across versions (or change notice brought up between updates):
    - Ext.data.Store.getNewRecords()
    - Ext.data.Store.getUpdatedRecords()
    - Ext.data.Store.getRemovedRecords()

    Hope this helps!
    Fabrício Murta
    Developer & Support Expert
  4. #4
    Hi @fabricio.murta,

    Thank you for your reply!

    So I am not too sure about the concept of temporary storage that the store uses, but what I know is that. Since these records haven't been modified yet on the database or are even saved to the database. The store has a temporary PK that is coming up negative or what the actual term would be is the idProperty is coming up negative. After I click save on the mainTab then afterwards the idProperty will change with the actual PK of that item.

    So I am mainly working with temporary storage of a store.
  5. #5
    Hello again, @juan!

    I don't think this negative id behavior is documented, supported or even implemented by the Ext JS part. Isn't it part of your application business logic (to add new records, before saving to database, with negative IDs)?

    If you're relying on the negative IDs in Ext JS, you're treading a dangerous field, as they provide methods to check whether a store has new/removed/modified records, that's how you're supposed (and guaranteed) to get that information. But I really don't think these negative IDs are coming from Ext JS, but your application's logic.

    But no matter what, in the end I'm not sure the methods shared in our last post help you or not, as they are supposed to return the information I understand you need (whether the store has changed). Maybe it misses clearing the 'dirty' state (returning no new, updated or removed records) after you click "save" in your mainTab?
    Fabrício Murta
    Developer & Support Expert

Similar Threads

  1. [CLOSED] insert to grid store not showing red dirty flag
    By susanz in forum 4.x Premium Help
    Replies: 3
    Last Post: Jun 27, 2017, 2:50 PM
  2. [CLOSED] How to detect datefield changed
    By jchau in forum 2.x Premium Help
    Replies: 1
    Last Post: Sep 27, 2013, 9:50 AM
  3. Replies: 4
    Last Post: Mar 13, 2013, 1:34 AM
  4. Replies: 2
    Last Post: Jun 06, 2012, 9:27 PM
  5. [CLOSED] detect new store record on client side
    By pschojer in forum 1.x Premium Help
    Replies: 1
    Last Post: Jul 14, 2009, 9:42 AM

Tags for this Thread

Posting Permissions