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