JSON Structure

Hello,
I have a Tree Grid that loads data from RestDataSource. However, when I view that Tree, all I get is “No items to show”. I checked the Console and it does show the response (as below). I am expecting to show the top 7 folders, then when the user clicks on it, I intend to show their children.
Please also find the JS code.

[{
    text:'Folder 1',
	articleID: 1,
	parentArticleID: null
},
{
    text:'Folder 2',
	articleID: 2,
	parentArticleID: null
},
{
    text:'Folder 3',
	articleID: 3,
	parentArticleID: null
},
{
    text:'Folder 4',
	articleID: 4,
	parentArticleID: null
},
{
    text:'Folder 5',
	articleID: 5,
	parentArticleID: null
},
{
    text:'Folder 6',
	articleID: 6,
	parentArticleID: null
},
{
    text:'Folder 7',
	articleID: 7,
	parentArticleID: null
}]

The JS code (I tried both XJSONDataSource and also RESTDataSource, both same result – although not sure which one I should use anyway)

isc.XJSONDataSource.create({
ID:"articles",
fields:[
		{name: "text", type: "string"},
		{name: "articleID", type: "integer", primaryKey:true},
		{name: "parentArticleID", type: "integer", foreignKey: "articleID"}
	],

dataFormat: "json", dataURL : "/extjs/ext-2.0.2/examples/tree/treetest_json_parentsonly.json"
});


isc.TreeGrid.create({
    ID: "ArticleTree",
//    width: 500,
//    height: 400,
    autoFetchData: true,
    nodeIcon:"icons/16/person.png",
    folderIcon:"icons/16/person.png",
    showOpenIcons:false,
    showDropIcons:false, showHeader:false, 
    closedIconSuffix:"",
    data: isc.ResultTree.create({
        nameProperty: "text",
    	dataSource: articles
    }),
	showHover:true, canHover:true, 
fields:[
		{name: "text", type: "string"}
	]
	

});

Can you please tell me if the format of JSON isn’t something that SmartClient can handle? I am generating this JSON from Ruby on Rails program (currently I am passing in a JSON file with data that was generated by rails).
thanks a lot.

The first problem is that you provided a DataSource to the ResultTree, but not the TreeGrid. You can remove your ResultTree declaration entirely and, having provided a dataSource and set autoFetchData, one will be created automatically. If you need to set properties on the automatically created ResultTree, use TreeGrid.dataProperties.

thanks, seems correct, but didn’t work.

Thanks a lot for the solution.

However, I get an error now, if I move the datasource to TreeGrid, instead of ResultTree

9:56:32.656:XRP9:WARN:Log:SyntaxError: invalid label
19:56:32.843:XRP9:WARN:ViewLoader:isc_ViewLoader_51:setView() not explicitly called by loaded view and could not be autodetected for view: isc_ViewLoader_51

UPDATE: here’s the new code

isc.TreeGrid.create({
    ID: "ArticleTree",
//    width: 500,
//    height: 400,
    autoFetchData: true,
    nodeIcon:"icons/16/person.png",
    folderIcon:"icons/16/person.png",
    showOpenIcons:false,
    showDropIcons:false, showHeader:false, 
    closedIconSuffix:"",
        nameProperty: "text",
    	dataSource: articles,
	showHover:true, canHover:true, 
fields:[
		{name: "text", type: "string"}
	]
	

});

However, if I try add response attribute to the JSON, it seems to work fine.

{ response: {
	status: 0,
    data: [
{
    text:"Folder 1",
	articleID: 1,
	parentArticleID: null
},
{
    text:"Folder 2",
	articleID: 2, 
	parentArticleID: null
},
{
    text:"Folder 3",
	articleID: 3,
	parentArticleID: null
},
{
    text:"Folder 4",
	articleID: 4,
	parentArticleID: null
},
{
    text:"Folder 5",
	articleID: 5,
	parentArticleID: null
},
{
    text:"Folder 6",
	articleID: 6,
	parentArticleID: null
},
{
    text:"Folder 7",
	articleID: 7,
	parentArticleID: null
}
]}
}

My question is
a) Should the JSOn structure have response, status and data elements?
b) Since I am using Rails, the JSON that it generates lists the attributes/values directly, without the response/status/data. Is there any way to make the Tree accept that format?

thanks a lot again. Appreciate your help.

Hi sriram,

The DSDataFormat docs mention the expected JSON data format - the problem in your case was that the outermost object was an Array.

You could work with the existing format completely unchanged by setting dataFormat:“custom” and doing the eval() yourself within dataSource.transformResponse(). However this might be wasted work, as down the road it’s likely that you’ll want to reintroduce metadata flags like “status” to deal with things like relogin and/or to build intelligent client-side error handling.

Thanks Isomorphic. I think that would work. For now, being new to both SmartClient and RubyonRails, I’ll try the easiest possible way (bad design practice, but can’t help it).

I was actually also looking at using transformresponse for JSON, but it seems like it returns the data element to the function, which I don’t have.

By the way, is the response/status/data, a standard JSON practice or is it only enforced by Smartclient?

UPDATE: From the documentation (http://www.smartclient.com/docs/6.0/a/b/c/go.html#method..DataSource.transformResponse), it seems like SmartClient will give me dsResponse.data in raw format. But, is this data the same as response/data (which again I don’t have)? Or I am just getting myself confused?

"dataFormat:“custom” : dsResponse.data is the raw response in String form. It must be parsed into an Array of Objects for subsequent processing to work.

“data” is the String returned over HTTP by Ruby, that is "{[ text:'Folder 1 … ". Something like eval(data) will give you an Array from this String.

“response” / “status” / “data” come specifically from RestDataSource.
RestDataSource is intended for people who have no existing data formats or data services, and provides a “cookbook” data protocol that supports all of SmartClient data layer features (data paging, sort, error handling, cache updates, etc), so that all you have to do is implement a server that provides the expected responses.

More generally, SmartClient can adapt to any pre-existing XML, JSON or other format at the DataSource level without any change to UI code. For example, some SmartClient examples show data paging, validation error handing, etc being used with SalesForce web services.

Thank you very much for almost real-time responses. You guys are superb at that.

OK. I changed my code to

isc.RestDataSource.create({
ID:"articles",
fields:[
		{name: "text", type: "string"},
		{name: "articleID", type: "integer", primaryKey:true},
		{name: "parentArticleID", type: "integer", foreignKey: "articleID"}
	],

//fetchDataURL:"/inno/loadAllArticles.jsp"
//dataFormat: "json", dataURL : "treetest_json_parentsonly.htm"
dataFormat: "custom", dataURL : "treetest_json_parentsonly.htm",
	transformReponse: function(dsResponse, dsRequest, xmlData) {
//    	console.warn("dsResponse is " + dsResponse.data);
//        console.warn("xmlData is " + xmlData);
		return dsResponse.data;
    }
});

The .htm URL now returns only data like RoR would.

However, i get the following error in Firebug/Firefox. In Console, all I get is
21:00:14.015:XRP2:WARN:Log:TypeError: _3.selectString is not a function

Any ideas why I am not ‘catching’ the data right? thanks.

_3.selectString is not a function
transformResponse(Object, Object operationType=fetch dataSource=articles data=Object, "{\r\n text:"Folder 1",\r\n articleID: 1,\r\n parentArticleID: null\r\n},\r\n{\r\n text:"Folder 2",\r\n artic...")ISC_DataBinding.j... (line 658)
38b("{\r\n text:"Folder 1",\r\n articleID: 1,\r\n parentArticleID: null\r\n},\r\n{\r\n text:"Folder 2",\r\n artic...", Object, Object operationType=fetch dataSource=articles data=Object, Object operationId=articles_fetch clientContext=Object, Object operationType=fetch dataSource=articles)ISC_DataBinding.j... (line 263)
50e(Object operationId=articles_fetch clientContext=Object, "{\r\n text:"Folder 1",\r\n articleID: 1,\r\n parentArticleID: null\r\n},\r\n{\r\n text:"Folder 2",\r\n artic...", Object operationType=fetch dataSource=articles)ISC_DataBinding.j... (line 187)
getInstanceProperty(Object target=[RestDataSource ID:articles], "rpcResponse,data,rpcRequest", [Object operationId=articles_fetch clientContext=Object, "{\r\n text:"Folder 1",\r\n articleID: 1,\r\n parentArticleID: null\r\n},\r\n{\r\n text:"Folder 2",\r\n artic...", Object operationType=fetch dataSource=articles], [RestDataSource ID:articles] ns=Object ID=articles fields=Object, undefined)ISC_Core.js (line 163)
convertToMethod(Object target=[RestDataSource ID:articles], "rpcResponse,data,rpcRequest", [Object operationId=articles_fetch clientContext=Object, "{\r\n text:"Folder 1",\r\n articleID: 1,\r\n parentArticleID: null\r\n},\r\n{\r\n text:"Folder 2",\r\n artic...", Object operationType=fetch dataSource=articles], undefined)ISC_Core.js (line 208)
fireReplyCallback(Object target=[RestDataSource ID:articles], Object operationType=fetch dataSource=articles, Object operationId=articles_fetch clientContext=Object, "{\r\n text:"Folder 1",\r\n articleID: 1,\r\n parentArticleID: null\r\n},\r\n{\r\n text:"Folder 2",\r\n artic...")ISC_DataBinding.j... (line 445)
fireReplyCallback(Object operationType=fetch dataSource=articles, Object operationId=articles_fetch clientContext=Object)ISC_DataBinding.j... (line 445)
performOperationReply(Object operationType=fetch dataSource=articles, Object operationId=articles_fetch clientContext=Object)ISC_DataBinding.j... (line 445)
responseRequiresLogin(11)ISC_DataBinding.j... (line 443)
performTransactionReply(11, "{\r\n text:"Folder 1",\r\n articleID: 1,\r\n parentArticleID: null\r\n},\r\n{\r\n text:"Folder 2",\r\n artic...", undefined)ISC_DataBinding.j... (line 436)
aq(11, XMLHttpRequest, undefined)ISC_Core.js (line 32)
getInstanceProperty("isc.RPCManager.performTransactionReply(transactionNum,results,wd)", "transactionNum,results,wd", [11, XMLHttpRequest], Window inno.htm, undefined)ISC_Core.js (line 163)
performXmlTransactionReply(11, XMLHttpRequest)ISC_Core.js (line 545)
aq(XMLHttpRequest)ISC_Core.js (line 32)
getInstanceProperty("isc.Comm.performXmlTransactionReply(11, xmlHttpRequest)", "xmlHttpRequest", [XMLHttpRequest], Window inno.htm, true)ISC_Core.js (line 163)
h0(XMLHttpRequest, "isc.Comm.performXmlTransactionReply(11, xmlHttpRequest)")ISC_Core.js (line 528)
_12()ISC_Core.js (line 534)
[Break on this error] if(_4.totalRows!=null)_1.totalRows=_4.totalRows;if(_4.startRow!=null)_1.startRow...

You want:

        ...
        dataFormat: "custom", dataURL : "treetest_json_parentsonly.htm",
	transformReponse: function(dsResponse, dsRequest, jsonString) {
                dsResponse.data = eval(jsonString);
                return dsResponse;
        }
});

Sorry, same error (21:25:13.281:XRP4:WARN:Log:TypeError: _3.selectString is not a function
)
I have:

dataFormat: "custom", dataURL : "treetest_json_parentsonly.htm",
	transformReponse: function(dsResponse, dsRequest, jsonString) {
		dsResponse.data = eval(jsonString);
//        console.warn("dsResponse data is " + eval(jsonString));
		return dsResponse;
    }
});

I apologize for this lengthy back-and-forth of messages. thank you for your patience.

Here’s the error again (forgot to attach)

_3.selectString is not a function
transformResponse(Object, Object operationType=fetch dataSource=articles data=Object, "[\r\n{\r\n text:"Folder 1",\r\n articleID: 1,\r\n parentArticleID: null\r\n},\r\n{\r\n text:"Folder 2",\r\n ar...")ISC_DataBinding.j... (line 658)
38b("[\r\n{\r\n text:"Folder 1",\r\n articleID: 1,\r\n parentArticleID: null\r\n},\r\n{\r\n text:"Folder 2",\r\n ar...", Object, Object operationType=fetch dataSource=articles data=Object, Object operationId=custom clientContext=Object, Object operationType=fetch dataSource=articles)ISC_DataBinding.j... (line 263)
50e(Object operationId=custom clientContext=Object, "[\r\n{\r\n text:"Folder 1",\r\n articleID: 1,\r\n parentArticleID: null\r\n},\r\n{\r\n text:"Folder 2",\r\n ar...", Object operationType=fetch dataSource=articles)ISC_DataBinding.j... (line 187)
getInstanceProperty(Object target=[RestDataSource ID:articles], "rpcResponse,data,rpcRequest", [Object operationId=custom clientContext=Object, "[\r\n{\r\n text:"Folder 1",\r\n articleID: 1,\r\n parentArticleID: null\r\n},\r\n{\r\n text:"Folder 2",\r\n ar...", Object operationType=fetch dataSource=articles], [RestDataSource ID:articles] ns=Object ID=articles fields=Object, undefined)ISC_Core.js (line 163)
convertToMethod(Object target=[RestDataSource ID:articles], "rpcResponse,data,rpcRequest", [Object operationId=custom clientContext=Object, "[\r\n{\r\n text:"Folder 1",\r\n articleID: 1,\r\n parentArticleID: null\r\n},\r\n{\r\n text:"Folder 2",\r\n ar...", Object operationType=fetch dataSource=articles], undefined)ISC_Core.js (line 208)
fireReplyCallback(Object target=[RestDataSource ID:articles], Object operationType=fetch dataSource=articles, Object operationId=custom clientContext=Object, "[\r\n{\r\n text:"Folder 1",\r\n articleID: 1,\r\n parentArticleID: null\r\n},\r\n{\r\n text:"Folder 2",\r\n ar...")ISC_DataBinding.j... (line 445)
fireReplyCallback(Object operationType=fetch dataSource=articles, Object operationId=custom clientContext=Object)ISC_DataBinding.j... (line 445)
performOperationReply(Object operationType=fetch dataSource=articles, Object operationId=custom clientContext=Object)ISC_DataBinding.j... (line 445)
responseRequiresLogin(10)ISC_DataBinding.j... (line 443)
performTransactionReply(10, "[\r\n{\r\n text:"Folder 1",\r\n articleID: 1,\r\n parentArticleID: null\r\n},\r\n{\r\n text:"Folder 2",\r\n ar...", undefined)ISC_DataBinding.j... (line 436)
aq(10, XMLHttpRequest, undefined)ISC_Core.js (line 32)
getInstanceProperty("isc.RPCManager.performTransactionReply(transactionNum,results,wd)", "transactionNum,results,wd", [10, XMLHttpRequest], Window inno.htm, undefined)ISC_Core.js (line 163)
performXmlTransactionReply(10, XMLHttpRequest)ISC_Core.js (line 545)
aq(XMLHttpRequest)ISC_Core.js (line 32)
getInstanceProperty("isc.Comm.performXmlTransactionReply(10, xmlHttpRequest)", "xmlHttpRequest", [XMLHttpRequest], Window inno.htm, true)ISC_Core.js (line 163)
h0(XMLHttpRequest, "isc.Comm.performXmlTransactionReply(10, xmlHttpRequest)")ISC_Core.js (line 528)
_12()ISC_Core.js (line 534)
[Break on this error] if(_4.totalRows!=null)_1.totalRows=_4.totalRows;if(_4.startRow!=null)_1.startRow...

Sorry, that code is intended for just a DataSource, not a RestDataSource.

Do you mean I can use a DataSource then? thanks.
But then again, I thought the documentation said that “custom” dataFormat is supported?

Yes, isc.DataSource.create(). Nothing else about the definition should need to change.

Cool. The error is now gone (thanks), but I still don’t get the data loaded into the tree. Even an alert and console.warn do not produce any output. I am perplexed…

dataFormat: "custom", dataURL : "treetest_json_parentsonly.htm",
	transformReponse: function(dsResponse, dsRequest, jsonString) {
		dsResponse.data = eval(jsonString);
        console.warn("dsResponse data is " + eval(jsonString));
		alert(dsResponse.data);
		return dsResponse;
    }
});

You have a typo, which I unfortunately echo’d back to you. transformResponse

You’re the best. Thank you very much. I haven’t seen any product’s support being this patient and helpful.