PDA

View Full Version : TreePanel "chops" output from .Net Loader



cyclops
23 Jun 2010, 1:06 AM
Back to the old "Trees suck but I've got to use them" issue...

I'm trying to load nodes from a .Net WebHander Page (ashx) but the tree displays each individual character of my output as folder (in Fifo 3.6.3, IE 8 doesn't render any children). Still the output seems to be correct as Firebug shows the array in it's JSON view as expected.

Here's my tree config:



var treePanel = new Ext.tree.TreePanel({
width: 450,
region: 'west',
useArrows: true,
autoScroll: true,
enableDD: false,
id: 'document_tree',
containerScroll: true,
border: false,
dataUrl: 'GetTemplateTree.ashx',
rootVisible: false,
root: {
nodeType: 'async',
text: 'Dokumente',
cls: 'folder',
id: '0'
}
});
That's what my Loader Page returns:



[{"id":"1","cls":"folder","leaf":false,"text":"Hallo"},{"id":"2","cls":"folder","leaf":false,"text":"Selber Hallo"}]
The returned content type is: application/json; charset=utf-8 (but neither text/html nor a different charset make any difference).

My ashx-file is declared as follows (just the essentials):



public void ProcessRequest(HttpContext context) {
var tree = new List<ExtJsTreeNode> {
new ExtJsTreeNode {text = "Hallo", id = "1", cls = "folder", leaf = false},
new ExtJsTreeNode {text = "Selber Hallo", id = "2", cls = "folder", leaf = false}
};
context.Response.ContentType = "application/json";
var serializer = new JavaScriptSerializer();
context.Response.Write(serializer.Serialize(tree));
}


(ExtJsTreeNode is one of my own helper classes, merely a struct).

Maybe I'm missing something really basic - hope you've got an idea.

Regards
Markus

Condor
23 Jun 2010, 1:34 AM
Are you sure that this is the exact response from your server?

cyclops
23 Jun 2010, 2:49 AM
Well that's what I copied from firebug...

I just tested this on a "real" webserver running IIS 6.0 and it works fine - seems this is only an issue to my local test (IIS 5.1) and development (ASP.NET Development Server/9.0.0.0) - ???

cyclops
23 Jun 2010, 4:58 AM
I eventually solved this by subclassing the TreeLoader and using simple eval instead of Ext.decode, I will continue to investigate this but for now it solved my problem.



//tree loader for consumption of ashx output
Ext.namespace('Ext.qs.tree');

Ext.qs.tree.AshxLoader = function(config) {
//Calls our super-class (Ext.data.DataProxy) constructor for full initialization
Ext.qs.tree.AshxLoader.superclass.constructor.call(this, config);

};

Ext.extend(Ext.qs.tree.AshxLoader, Ext.tree.TreeLoader, {
processResponse: function(response, node, callback, scope) {
var json = response.responseText;
try {
//this is the original line
//var o = response.responseData || Ext.decode(json);
var o = eval(json);
node.beginUpdate();
for (var i = 0, len = o.length; i < len; i++) {
var n = this.createNode(o[i]);
if (n) {
node.appendChild(n);
}
}
node.endUpdate();
this.runCallback(callback, scope || node, [node]);
} catch (e) {
this.handleFailure(response);
}
}
});
Maybe you've got an idea where to start to find out what the real problem is?

Condor
23 Jun 2010, 5:01 AM
eval() and Ext.decode() are exactly the same (unless you set Ext.USE_NATIVE_JSON=true).

cyclops
23 Jun 2010, 5:48 AM
Indeed they are. Checking further I remembered using a script that overrides Ext.decode (Ryan Moore's datefix) to handle the unusual dateformat of DotNet-Webservices (hasn't been solved any other way, er?). It just didn't take into account JSON strings may start with square brackets as well as curly ones. After changing this in the datefix script everythings working fine again - sorry for wasting your time (:|



this.decode = function(json) {
var obj = json;
var type = typeof json;
//Application.log(type + ' ' + json);
if (type == 'string') {
//original line
//if (json.indexOf('{') == 0) {
if (json.indexOf('{') == 0 || json.indexOf('[') == 0) {
return eval('(' + json + ')');
}
else {
return eval('(\'' + json + '\')');
}
}
};
})();