1. #1
    Sencha User willf1976's Avatar
    Join Date
    May 2009
    Location
    carpinteria, ca
    Posts
    84
    Vote Rating
    0
    willf1976 is on a distinguished road

      0  

    Default Ext.ux.tree.TreePanel.toObject

    Ext.ux.tree.TreePanel.toObject


    Hi All

    I have been going through the classes we have developed over the last few years and sharing back with the community some of them that I think will be useful to others. These components have been tested extensively in ext-3.2.1.

    Here is: Ext.ux.tree.TreePanel.toObject

    This the newest version of the code I use to turn a tree node into an object structure. It is used by these other plugins I have authored:

    http://www.sencha.com/forum/showthread.php?77100

    http://www.sencha.com/forum/showthread.php?77583

    It is a modified version of

    dpasichnik's treeSerializer found at:
    http://www.extjs.com/forum/showthread.php?t=20793

    Code:
    /**
     * @authors dpasichnik, Will Ferrer, Ethan Brooks
     * @copyright (c) 2012, Intellectual Property Private Equity Group
     * @licensee 2012 developed under license for Switchsoft LLC http://www.switchsoft.com a "Direct response telephony company" as part of it's "VOIP Call distribution, ROI analysis platform, call recording, and IVR for inbound and outbound sales" and Run the Business Systems LLC a "Technology development investment group" as part of it's "PHP, Javascript rapid application development framework and MySQL analysis tools"
     * @license http://extjs.com/license
     * licensed development library or toolkit without explicit permission.
     * <p>License details: <a href="http://www.gnu.org/licenses/lgpl.html"
     * target="_blank">http://www.gnu.org/licenses/lgpl.html</a></p>
    * We are pretty nice just ask. We want to meet our licensees
    */
    /*
    original author: dpasichnik
    modified by: Will Ferrer
    date: 09/03/09
    history:
        09/03/09 -- posted to ExtJS forums
    */
    /**
        This code is based on dpasichnik's treeSerializer found at:
        http://www.extjs.com/forum/showthread.php?t=20793
    */
    /**
     * Returns a object that represents the tree
     * @param {Function} nodeFilter (optional) A function, which when passed the node, returns true or false to include
     * or exclude the node.
     * @param {Function} attributeFilter (optional) A function, which when passed an attribute name, and an attribute value,
     * returns true or false to include or exclude the attribute.
     * @param {Object} attributeMapping (optional) An associative array which can you use to remap attribute names on the nodes as they are converted to an object. Keys in the array represent attributes to be remapped, and their associated values represent the new keys that those attributes will be remapped onto in the returned object.
     * @param {Object} scope (optional) The scope to call the filter functions in. Defaults to this.
     * @return {String}
     */
    Ext.tree.TreePanel.prototype.toObject = function(nodeFilter, attributeFilter, attributeMapping, scope){
        return this.getRootNode().toObject(nodeFilter, attributeFilter, attributeMapping, scope);
    };
    
    /**
     * Returns an object that represents the node
     * @param {Function} nodeFilter (optional) A function, which when passed the node, returns true or false to include
     * or exclude the node.
     * @param {Function} attributeFilter (optional) A function, which when passed an attribute name, and an attribute value,
     * returns true or false to include or exclude the attribute.
     * @param {Object} attributeMapping (optional) An associative array which can you use to remap attribute names on the nodes as they are converted to an object. Keys in the array represent attributes to be remapped, and their associated values represent the new keys that those attributes will be remapped onto in the returned object.
     * @return {String}
     */
    
    Ext.tree.TreeNode.prototype.toObject = function(nodeFilter, attributeFilter, attributeMapping, scope){
        var scope = (!Ext.isEmpty(scope))?scope:this;
        
    //    Exclude nodes based on caller-supplied filtering function
        if (nodeFilter && (nodeFilter.call(scope, this) == false)) {
            return undefined;
        }
        var c = false, returnObj = {};
    
    //    Add the id attribute unless the attribute filter rejects it.
        if (!attributeFilter || attributeFilter.call(scope, "id", this.id)) {
            returnObj.id = this.id;
            c = true;
        }
    
    //    Add all user-added attributes unless rejected by the attributeFilter.
        for(var key in this.attributes) {
            if ((key != 'id') && (!attributeFilter || attributeFilter.call(scope, key, this.attributes[key]))) {
                if (attributeMapping && attributeMapping[key]) {
                    thisKey = attributeMapping[key];
                } else {
                    thisKey = key;
                }
                returnObj[thisKey] = this.attributes[key];
                c = true;
            }
        }
    //    Add child nodes if any
        var children = this.childNodes;
        var clen = children.length;
        returnObj['children'] = [];
        if(clen != 0){
            for(var i = 0; i < clen; i++){
                returnObj['children'][i] = children[i].toObject(nodeFilter, attributeFilter, attributeMapping, scope);
            }
        }
        return returnObj;
    };
    
    
    /**
     * preloads a tree so that all its child nodes are created
     * @return {Boolean}
     */
    Ext.tree.TreePanel.prototype.prepareNodes = function(){
        return this.getRootNode().prepareNode();
    };
    
    /**
     * Returns an object that represents the node
     * @return {Boolean}
     */
    
    Ext.tree.TreeNode.prototype.prepareNode = function(){
        var loader = this.loader || this.attributes.loader || this.getOwnerTree().getLoader();
        loader.load(this, /*this.loadComplete.createDelegate(this, [false, false, null, null])*/null, this);
        
        var children = this.childNodes;
        var clen = children.length;
    
        if(clen != 0){
            for(var i = 0; i < clen; i++){
                children[i].prepareNode();
            }
        }
        return true;
    };
    Best regards

    Will Ferrer (Run the Business)

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,650
    Vote Rating
    899
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    have seen people wanting to do things like this, in Ext JS 4 we are going to be supporting serializing a treestore.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    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 in print!

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

  3. #3
    Sencha User willf1976's Avatar
    Join Date
    May 2009
    Location
    carpinteria, ca
    Posts
    84
    Vote Rating
    0
    willf1976 is on a distinguished road

      0  

    Default


    Hi Mitchell

    Yes it has been a very useful functionality for us as we serialize the contents of trees and store them in our database for latter use.

    I am very glad you guys have it planed for version 4.

    I hope this message finds you well.

    Best regards

    Will Ferrer

Thread Participants: 1

Tags for this Thread