PDA

View Full Version : Loading model data into a tree



dave_e
28 Nov 2011, 9:59 AM
Hi,

I'm trying to load some existing data which is structured as a tree, display it in a TreePanel and be able to edit it's content and re-save it again.

The data is available over HTTP at urls like :

http://example.com/data/nodes - returns a list of all nodes
http://example.com/data/nodes/1 returns node with id=1

The response from http://example.com/data/nodes looks something like this, and the response from http://example.com/data/nodes/1 is identical except that only a single node value will be returned.



{
"response": [{
"id": 1,
"name": "node-name",
"values": "some data here"
"parent": null,
"children": [2,3]
}, {
"id": 2,
"name": "node-name",
"values": "some data here"
"parent": 1,
"children": []
}, {
"id": 3,
"name": "node-name",
"values": "some data here"
"parent": 1,
"children": []
}]
}


So far, I've found it impossible to set up a TreeStore, Model and Proxy that can load this data and make it available as a hierarchical data structure. Is this possible?

I have managed to load the data if I modify the response to return the full hierarchy e.g. http://example.com/data/nodes/1 will return:



{
"response": [{
"id": 1,
"name": "node-name",
"values": "some data here"
"parent": null,
"children": [{
"id": 2,
"name": "node-name",
"values": "some data here"
"parent": 1,
"children": []
},{
"id": 3,
"name": "node-name",
"values": "some data here"
"parent": 1,
"children": []
}]
}]
}


However, that has other issues. For example, if I edit node 3 then the values stored in the records in the childNodes property is updated, but (presumably) because this is a single Model the data that is sent back to the server is in from the data property which hasn't been updated.

This seems like it would be a fairly common use-case (don't everybody's problems) so I'm hoping there is 'right' way to handle this.

Thanks,
Dave

mitchellsimoens
28 Nov 2011, 12:08 PM
If you want async nodes (only those that are expanded are loaded) then you should return those children of that node. Root is a node. There is an XML Tree example that you could see the response and translate it to JSON.

dave_e
28 Nov 2011, 1:52 PM
Thanks Mitchell, I'm actually trying to load the entire tree up front.

Basically I'm loading and editing a document which is a tree structure. Each node of the tree is available as a resource on a url.

There don't seem to be any examples where a complete tree is loaded from Model data.