Fix #5665 - pre-commit hooks for roojs
[roojs1] / Roo / tree / TreeLoader.js
index 89c5aff..d2c93e2 100644 (file)
  * nodes from a specified URL. The response must be a javascript Array definition
  * who's elements are node definition objects. eg:
  * <pre><code>
-   [{ 'id': 1, 'text': 'A folder Node', 'leaf': false },
-    { 'id': 2, 'text': 'A leaf Node', 'leaf': true }]
+{  success : true,
+   data :      [
+   
+    { 'id': 1, 'text': 'A folder Node', 'leaf': false },
+    { 'id': 2, 'text': 'A leaf Node', 'leaf': true }
+    ]
+}
+
+
 </code></pre>
  * <br><br>
+ * The old style respose with just an array is still supported, but not recommended.
+ * <br><br>
+ *
  * A server request is sent, and child nodes are loaded only when a node is expanded.
  * The loading node's id is passed to the server under the parameter name "node" to
  * enable the server to produce the correct child nodes.
@@ -29,7 +39,9 @@
     myTreeLoader.on("beforeload", function(treeLoader, node) {
         this.baseParams.category = node.attributes.category;
     }, this);
-</code></pre><
+    
+</code></pre>
+ *
  * This would pass an HTTP parameter called "category" to the server containing
  * the value of the Node's "category" attribute.
  * @constructor
@@ -42,6 +54,7 @@ Roo.tree.TreeLoader = function(config){
     Roo.apply(this, config);
 
     this.addEvents({
+    
         /**
          * @event beforeload
          * Fires before a network request is made to retrieve the Json text which specifies a node's children.
@@ -49,7 +62,7 @@ Roo.tree.TreeLoader = function(config){
          * @param {Object} node The {@link Roo.tree.TreeNode} object being loaded.
          * @param {Object} callback The callback function specified in the {@link #load} call.
          */
-        "beforeload" : true,
+        beforeload : true,
         /**
          * @event load
          * Fires when the node has been successfuly loaded.
@@ -57,7 +70,7 @@ Roo.tree.TreeLoader = function(config){
          * @param {Object} node The {@link Roo.tree.TreeNode} object being loaded.
          * @param {Object} response The response object containing the data from the server.
          */
-        "load" : true,
+        load : true,
         /**
          * @event loadexception
          * Fires if the network request failed.
@@ -65,7 +78,14 @@ Roo.tree.TreeLoader = function(config){
          * @param {Object} node The {@link Roo.tree.TreeNode} object being loaded.
          * @param {Object} response The response object containing the data from the server.
          */
-        "loadexception" : true
+        loadexception : true,
+        /**
+         * @event create
+         * Fires before a node is created, enabling you to return custom Node types 
+         * @param {Object} This TreeLoader object.
+         * @param {Object} attr - the data returned from the AJAX call (modify it to suit)
+         */
+        create : true
     });
 
     Roo.tree.TreeLoader.superclass.constructor.call(this);
@@ -78,6 +98,11 @@ Roo.extend(Roo.tree.TreeLoader, Roo.util.Observable, {
     * to be loaded.
     */
     /**
+    * @cfg {String} requestMethod either GET or POST
+    * defaults to POST (due to BC)
+    * to be loaded.
+    */
+    /**
     * @cfg {Object} baseParams (optional) An object containing properties which
     * specify HTTP parameters to be passed to each request for child nodes.
     */
@@ -88,6 +113,8 @@ Roo.extend(Roo.tree.TreeLoader, Roo.util.Observable, {
     */
     /**
     * @cfg {Object} uiProviders (optional) An object containing properties which
+    * 
+    * DEPRECATED - use 'create' event handler to modify attributes - which affect creation.
     * specify custom {@link Roo.tree.TreeNodeUI} implementations. If the optional
     * <i>uiProvider</i> attribute of a returned child node is a string rather
     * than a reference to a TreeNodeUI implementation, this that string value
@@ -186,10 +213,9 @@ Roo.extend(Roo.tree.TreeLoader, Roo.util.Observable, {
         }
     },
 
-    /**
-    * Override this function for custom TreeNode node implementation
-    */
-    createNode : function(attr){
+    // private
+    createNode : function(attr)
+    {
         // apply baseAttrs, nice idea Corey!
         if(this.baseAttrs){
             Roo.applyIf(attr, this.baseAttrs);
@@ -197,27 +223,47 @@ Roo.extend(Roo.tree.TreeLoader, Roo.util.Observable, {
         if(this.applyLoader !== false){
             attr.loader = this;
         }
+        // uiProvider = depreciated..
+        
         if(typeof(attr.uiProvider) == 'string'){
-            
            attr.uiProvider = this.uiProviders[attr.uiProvider] || 
                 /**  eval:var:attr */ eval(attr.uiProvider);
         }
         if(typeof(this.uiProviders['default']) != 'undefined') {
             attr.uiProvider = this.uiProviders['default'];
         }
+        
+        this.fireEvent('create', this, attr);
+        
         attr.leaf  = typeof(attr.leaf) == 'string' ? attr.leaf * 1 : attr.leaf;
         return(attr.leaf ?
                         new Roo.tree.TreeNode(attr) :
                         new Roo.tree.AsyncTreeNode(attr));
     },
 
-    processResponse : function(response, node, callback){
+    processResponse : function(response, node, callback)
+    {
         var json = response.responseText;
         try {
             
-            var o = /**  eval:var:zzzzzzzzzz */ eval("("+json+")");
+            var o = Roo.decode(json);
+            
+            if (this.root === false && typeof(o.success) != undefined) {
+                this.root = 'data'; // the default behaviour for list like data..
+                }
+                
+            if (this.root !== false &&  !o.success) {
+                // it's a failure condition.
+                var a = response.argument;
+                this.fireEvent("loadexception", this, a.node, response);
+                Roo.log("Load failed - should have a handler really");
+                return;
+            }
+            
+            
+            
             if (this.root !== false) {
-                o = o[this.root];
+                 o = o[this.root];
             }
             
             for(var i = 0, len = o.length; i < len; i++){
@@ -241,7 +287,9 @@ Roo.extend(Roo.tree.TreeLoader, Roo.util.Observable, {
         this.fireEvent("load", this, a.node, response);
     },
 
-    handleFailure : function(response){
+    handleFailure : function(response)
+    {
+        // should handle failure better..
         this.transId = false;
         var a = response.argument;
         this.fireEvent("loadexception", this, a.node, response);