1. #1
    Sencha User murrah's Avatar
    Join Date
    Oct 2007
    Location
    Katoomba, Blue Mountains, west of Sydney, Australia
    Posts
    315
    Vote Rating
    2
    murrah is on a distinguished road

      0  

    Default Tip: Remove records from store without triggering a proxy Destroy operation

    Tip: Remove records from store without triggering a proxy Destroy operation


    Hi,

    I had a situation where I needed to remove records from a store that got it's data via a REST proxy, but I didn't want to actually delete the records from the server, I just wanted them out of the store driving my grid.

    The way I was signally which records to remove from the grid / store was by dragging them onto a particular tree node and catching the drop in beforeDrop, removing the dropped records from the grid then cancelling the drop.

    I was using store suspendAutoSync() which didn't sync the remove to the server (good) but found that when I did a subsequent drop, the previously dropped and removed records were being deleted from the server via a mysterious destroy operation! (bad).

    I discovered that the reason was that the removed records were being saved in the store's removed[] collection in the remove() method. What this meant was that any subsequent proxy operation had a "pending" destroy operation waiting for an opportunity to run.

    I also noticed that if I passed a second parameter to the remove() method (private, so it is not in the docs), that would prevent the record from being added to the removed[] collection and hence there would be no pending destroy.

    This is my code:
    PHP Code:
    data.records = [array of records to remove from the store]

    var 
    subjStore this.getVSubjectGrid().store;

    subjStore.suspendAutoSync();

    Ext.Array.each(data.records, function (recindex) {
        
    subjStore.remove(rectrue); //<--- passed true here to solve the problem
    });

    subjStore.resumeAutoSync(); 
    The second parameter to the remove() method is 'isMove' and the relevant part of the remove() method is:
    PHP Code:
    // don't push phantom records onto removed
    if (!isMove && isNotPhantom) {

        
    // Store the index the record was removed from so that rejectChanges can re-insert at the correct place.
        // The record's index property won't do, as that is the index in the overall dataset when Store is buffered.
        
    record.removedFrom index;
        
    me.removed.push(record);

    By setting isMove to true, the record is not added to me.removed[].

    I hope someone else might find this useful.

    Cheers,
    Murray

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    St. Louis, MO
    Posts
    33,635
    Vote Rating
    435
    mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of

      0  

    Default


    Thanks for posting the tip for the community!
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    http://www.JSONPLint.com - Source to lint your JSONP!

    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User murrah's Avatar
    Join Date
    Oct 2007
    Location
    Katoomba, Blue Mountains, west of Sydney, Australia
    Posts
    315
    Vote Rating
    2
    murrah is on a distinguished road

      0  

    Default


    No worries - great to put something back!

    And.... I realised that the remove() can take an array so this is a slight simplification of the code above:
    PHP Code:
    var subjStore this.getVSubjectGrid().store;                
    subjStore.suspendAutoSync();
                
                
    // Set true so that the record is NOT added to the store.removed collection 
                // so it doesnt fire a later destroy operation on the record        
    subjStore.remove(data.records,true);
        
    subjStore.resumeAutoSync(); 
    ie no need for my loop.

    Cheers,
    Murray