1. #1
    Sencha Premium Member Zdeno's Avatar
    Join Date
    Nov 2009
    Location
    Prague
    Posts
    402
    Vote Rating
    17
    Zdeno will become famous soon enough

      1  

    Default rejectChanges - Ext.data.TreeStore

    rejectChanges - Ext.data.TreeStore


    Hello,

    i would like to ask you if anyone found way how to reject changes in treestore when you get exception on sync(). I already checked this forum but found information bout rejectChanges on Ext.data.Store only. So i wanted override Ext.data.treestore add rejectChanges function but i found one big problem.

    As you know property removed collecting all removed nodes with relations except relation to parent node in store. Basically you can rebuild tree from array removed but you dont have information bout relation to parent node or previous/nextSibling so you dont know where to this rebuilded tree put.

    Is this bug or can i read these information somewhere in structure?

  2. #2
    Sencha Premium Member Zdeno's Avatar
    Join Date
    Nov 2009
    Location
    Prague
    Posts
    402
    Vote Rating
    17
    Zdeno will become famous soon enough

      0  

    Default


    I think i found the problem? Function remove on node will call removeChild function with params: this, destroy, suppressEvents (NodeInterface.js: 844). Destroy tell us, if the node will be destroyed upon removal or not. Function removeChild on line 650-654 has condition:

    Code:
    if (destroy) {
        node.destroy(true);
    } else {
        node.clear();
    }
    So when i follow the code we need to know what does clear function do.

    Code:
    ...
    /**
     * Clears the node.
     * @private
     * @param {Boolean} [destroy=false] True to destroy the node.
     */
    clear : function(destroy) {
        var me = this;
    
    
        // clear any references from the node
        me.parentNode = me.previousSibling = me.nextSibling = null;
        if (destroy) {
            me.firstChild = me.lastChild = null;
        }
    },
    ...
    So how it looks, problem is in clear function. After we call this function, any references will be removed. So after that call you will loose all informations to reject changes.

    Anybody from Sencha can confirm this problem?

  3. #3
    Sencha - Community Support Team mankz's Avatar
    Join Date
    Nov 2007
    Location
    Stockholm, Sweden
    Posts
    2,761
    Vote Rating
    108
    mankz is a name known to all mankz is a name known to all mankz is a name known to all mankz is a name known to all mankz is a name known to all mankz is a name known to all

      0  

    Default


    +1, would be nice with a solution to this issue.

  4. #4
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    9,085
    Vote Rating
    467
    scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future

      0  

    Default


    This has been submitted: EXTJSIV-6804

    Regards,
    Scott.

  5. #5
    Ext JS Premium Member
    Join Date
    Jul 2011
    Posts
    17
    Vote Rating
    0
    adp-gmt is on a distinguished road

      0  

    Default


    Hello,
    I have the same problem (no rejectChanges method for TreeStore).
    Could you tell me more about the solution?
    Where can I find this EXTJSIV-6804?
    When I get an exception how to restore the node that the user wanted to remove or delete the node that the user wanted to add?


  6. #6
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    9,085
    Vote Rating
    467
    scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future

      0  

    Default


    This is an internal ID for our bug tracking software. At this time, it is slated for our 4.2 release (no public date).

    We do not provide public access to our tracking software at this time.

    Scott.

  7. #7
    Sencha User
    Join Date
    Dec 2011
    Posts
    1
    Vote Rating
    0
    andr-04 is on a distinguished road

      0  

    Default Ext JS 4.2.0 RC 1 (4.2.0.489)

    Ext JS 4.2.0 RC 1 (4.2.0.489)


    I have not found rejectChanges for Ext.data.TreeStore in RC 1. Will it be in RC 2 or later?

  8. #8
    Sencha User jean.timex's Avatar
    Join Date
    Jan 2008
    Posts
    2
    Vote Rating
    0
    jean.timex is on a distinguished road

      0  

    Default Alternative solution

    Alternative solution


    Recently I came across the same issue when using Ext 4.2.1, since TreeStore doesn't have the rejectChanges() function, here is what I did in store.sync's failure handler:
    Code:
    failure: function(batch, options) {
        Ext.each(batch.operations || [], function(operation) {
            Ext.each(operation.records || [], function(rec) {
                rec.reject();
            });
        });
    }
    So basically the idea is to call Model.reject() function.

  9. #9
    Sencha User VDP's Avatar
    Join Date
    Feb 2012
    Location
    Boom, Belgium
    Posts
    85
    Vote Rating
    9
    VDP will become famous soon enough

      0  

    Default


    +1 for an implementation of the rejectChanges on a treeStore

    Here is my impl (not fully tested yet, but it seems to work...)
    Fiddle
    Code:
    /**
     * @author Johan Vandeplas
     */
    Ext.define('My.overrides.data.NodeInterface', {
        override: 'Ext.data.NodeInterface',
        statics: {
            getPrototypeBody: function () {
                var me = this,
                    protBody = me.callParent(arguments),
                    removeFn = protBody.remove;
    
    
                protBody.remove = function () {
                    var me = this;
                    me.removedFromNode = me.parentNode;
                    me.removedFromIdx = me.parentNode.indexOf(me);
                    removeFn.apply(this, arguments);
                };
                return protBody;
            }
        }
    });
    
    
    Ext.define('My.overrides.data.TreeStore', {
        override: 'Ext.data.TreeStore',
    
    
        /**
         * removes node or an array of nodes from its parent.
         * @param {Ext.data.NodeInterface/[Ext.data.NodeInterface]}nodes
         * @param {Boolean} [destroy=false] True to destroy the node upon removal.
         */
        remove: function (nodes, destroy) {
            nodes = Ext.isArray(nodes) ? nodes : [nodes];
            Ext.each(nodes, function (node) {
                node.remove(destroy === true);
            });
        },
        /**
         * @returns {Array}
         */
        getRejectRecords: function () {
            return Ext.Array.push(this.getNewRecords(), this.getUpdatedRecords(), this.getRemovedRecords());
        },
        /**
         * brings the store to previous state (rejects all uncommitted changes)
         */
        rejectChanges: function () {
            var me = this,
                recs = me.getRejectRecords(),
                len = recs.length,
                i = 0,
                rec;
    
    
            for (; i < len; i++) {
                rec = recs[i];
                rec.reject();
                if (rec.phantom) {
                    me.remove(rec);
                }
            }
    
    
            // Restore removed records back to their original positions
            recs = me.removed;
            len = recs.length;
            for (i = len - 1; i >= 0; i--) {
                rec = recs[i];
                rec.removedFromNode.insertChild(rec.removedFromIdx, rec);
                delete rec.removedFromNode;
                delete rec.removedFromIdx;
                rec.reject();
            }
    
    
            // Since removals are cached in a simple array we can simply reset it here.
            // Adds and updates are managed in the data MixedCollection and should already be current.
            me.removed.length = 0;
        }
    });

  10. #10
    Sencha User
    Join Date
    Jul 2012
    Posts
    33
    Vote Rating
    0
    raminbp is on a distinguished road

      0  

    Default reject changes on exception for tree store

    reject changes on exception for tree store


    you can add an exception listener on the treeStore proxy like this:

    Code:
    this.getProxy().on({
                exception: function (proxy, response, operation) {
                    var modifiedRecord = me.getModifiedRecords(),
                        parentNode;
                    Ext.each(modifiedRecord, function (record) {
                        parentNode = record.parentNode;
                        record.reject();
                        parentNode.removeChild(record);
                    });
                    me.removed = [];
                }
            });