TreeGrid with RestDataSource

Ok, I had just about finished getting this post ready when I found the problem - and I thought it would be worth posting anyway as its pretty obscure.

***** START Original post *****

Been nutting away at this one for a while, and wonder if there’s not something obvious I’m missing. I’m trying to get a TreeGrid to connect to a RestDataSource that provides a flat record structure - so trying to use the foreignKey/rootValue approach - code follows

isc.defineClass("PersonaTree", "TreeGrid").addProperties({
    width: 500,
    height: 400,
    nodeIcon:"icons/16/person.png",
    folderIcon:"icons/16/person.png",
    showOpenIcons:false,
    showDropIcons:false,
    closedIconSuffix:"",
    autoFetchData:true,
    dataSource: personaData
});

isc.RestDataSource.create({
    ID: "personaData",
    dataFormat: "json",
    recordXPath: "response/data",
    fields:[
        {name : "key", valueXPath:"pk", primaryKey:"true"},
        {name : "userKey", valueXPath:"fields/user", foreignKey:"userData.key"},
        {name : "parentPersona", valueXPath:"fields/parentPersona"},
        {name : "parentId", valueXPath:"fields/parentId", foreignKey:"key", rootValue:"0"},
        {name : "isDefault", valueXPath:"fields/isDefault"},
        {name : "moniker", valueXPath:"fields/moniker"},
        {name : "created", valueXPath:"fields/created"}
	],
    fetchDataURL:"/data/persona/",
    addDataURL :"/data/persona/",
    updateDataURL:"/data/persona/",
    removeDataURL:"/data/persona/"
});

This is the relevant part of the SC log message

17:43:09.885:XRP8:INFO:xmlBinding:personaData:JSON recordXPath: '/response/data', selected: Array[2]
17:43:09.906:XRP8:DEBUG:xmlBinding:personaData:Validated dsResponse.data: [
    {
        pk:"agtkaXNvcy13ZWF2ZXINCxIHUGVyc29uYRgCDA", 
        model:"persona", 
        fields:{
            moniker:"Me", 
            created:"2008-10-30 17:41:12", 
            parentPersona:null, 
            user:"agtkaXNvcy13ZWF2ZXIKCxIEVXNlchgBDA", 
            parentId:"0", 
            isDefault:true
        }, 
        key:"agtkaXNvcy13ZWF2ZXINCxIHUGVyc29uYRgCDA", 
        userKey:"agtkaXNvcy13ZWF2ZXIKCxIEVXNlchgBDA", 
        parentPersona:null, 
        parentId:"0", 
        isDefault:true, 
        moniker:"Me", 
        created:"2008-10-30 17:41:12"
    }, 
    {
        pk:"agtkaXNvcy13ZWF2ZXINCxIHUGVyc29uYRgDDA", 
        model:"persona", 
        fields:{
            moniker:"Child", 
            created:"2008-10-30 17:41:12", 
            parentPersona:"agtkaXNvcy13ZWF2ZXINCxIHUGVyc29uYRgCDA", 
            user:"agtkaXNvcy13ZWF2ZXIKCxIEVXNlchgBDA", 
            parentId:"agtkaXNvcy13ZWF2ZXINCxIHUGVyc29uYRgCDA", 
            isDefault:false
        }, 
        key:"agtkaXNvcy13ZWF2ZXINCxIHUGVyc29uYRgDDA", 
        userKey:"agtkaXNvcy13ZWF2ZXIKCxIEVXNlchgBDA", 
        parentPersona:"agtkaXNvcy13ZWF2ZXINCxIHUGVyc29uYRgCDA", 
        parentId:"agtkaXNvcy13ZWF2ZXINCxIHUGVyc29uYRgCDA", 
        isDefault:false, 
        moniker:"Child", 
        created:"2008-10-30 17:41:12"
    }
]
17:43:09.933:XRP8:INFO:xmlBinding:personaData:dsResponse is: {data: Array[2],
startRow: 0,
status: 0,
endRow: 2,
totalRows: 2,
httpResponseCode: 200,
transactionNum: 1,
clientContext: Obj}
17:43:09.961:XRP8:WARN:treeLinking:isc_ResultTree_104:Couldn't find parents: agtkaXNvcy13ZWF2ZXIKCxIEVXNlchgBDA,agtkaXNvcy13ZWF2ZXIKCxIEVXNlchgBDA, unplaced children: agtkaXNvcy13ZWF2ZXINCxIHUGVyc29uYRgCDA,agtkaXNvcy13ZWF2ZXINCxIHUGVyc29uYRgDDA

Now I’d very much like to do away with the parentId field altogether, as it is almost identical to the ‘parentPersona’ field - the only difference being the former has a value of ‘0’ when the record is the root, and the latter has a value of null. I thought my issue might have been trying to match null for rootValue.

The logic I want is that if parentPersona==null, then the record is the root.

Anyway, it seems pretty clear that the data source is retrieving the data correctly, but I can’t find the right things to put in the datasource to get it to work - my TreeGrid just shows ‘Loading data…’ - and the logs seem pretty clear that this is because SC can’t establish the parent node. I’m sure it is just something stupid in the way I’ve specified the datasource - but it looks like it should work to me.

I guess there’s two questions here - what’s wrong with the code I’ve posted, and can I get rid of the parentId field and use the personaParent with its null value for root? I’m sure its just going to be brain fade on my part…

***** END original POST *****

The problem was that I had another foreign key (userKey) declared before the parentId field.

The log message did lead to the solution -

‘Couldn’t find parents: agtkaXNvcy13ZWF2ZXIKCxIEVXNlchgBDA’

That key was actually the user key, not the persona key - just tough to spot as the keys are so similar, and I wasn’t expecting the userKey to be involved.

The docs say the following -

‘The Tree.idField is derived from the dataSource you provide to the TreeGrid - the first field marked as DataSourceField.primaryKey:true becomes the idField of the ResultTree. The Tree.parentIdField is found by looking for a field that has a DataSourceField.foreignKey property pointing to the idField’

Now userKey is a foreignKey field, but it doesn’t point to the idField, so it should have been ignored in this context.

The following DataSource works fine

isc.RestDataSource.create({
    ID: "personaData",
    dataFormat: "json",
    recordXPath: "response/data",
    fields:[
        {name : "key", valueXPath:"pk", primaryKey:"true"},
        {name : "parentPersona", valueXPath:"fields/parentPersona", foreignKey:"key", rootValue:"null"},
        {name : "userKey", valueXPath:"fields/user", foreignKey:"userData.key"},
        {name : "isDefault", valueXPath:"fields/isDefault"},
        {name : "moniker", valueXPath:"fields/moniker"},
        {name : "created", valueXPath:"fields/created"}
	],
    fetchDataURL:"/data/persona/",
    addDataURL :"/data/persona/",
    updateDataURL:"/data/persona/",
    removeDataURL:"/data/persona/"
});

And I could lose the parentId field :slight_smile: Anyway, not sure if you consider that a bug, but might be worth updating the docs to be clearer about field ordering…

Hello hawkettc,

It looks like the docs are right and the behavior is wrong. We’ll take a look at this - thanks for posting both the original report and the working code.