I' am developing an multi User Application and I needed a kind of Sync mechanism for the Tree. May two or more User working in the same Tree and delete add or update Treenodes.

In the Current Version of the Tree you can ask the tree to re-request the Rootnode.
But there is no Way to just update the Tree...

So I decided to implement my own Updatemechanism.
I decided to Extend the Standart Ext.tree.Treeloader and to override the requestData and proccessresponse functions to handle updates.

You may Take a look into the code Documentation for more details...

Ext.ns('Ext', 'Ext.ux', 'Ext.ux.tree');

/**

* @class Ext.ux.tree.SyncableTreeLoader
* @extends Ext.tree.TreeLoader
*
*
* A SyncableTreeLoader provides the same functionality as its Baseclass (Ext.tree.Treeloader) plus the
* possibility of lazy updating an existing Structure of TreeNodes from a specified Url.
*
* The response must be a JavaScript Object definition whose Members are (by default) "deleted", "updated", "added"
*
* "deleted" contains a simple Javascript Array definition of the TreeNode-Id that should be deleted
*
* "updated" contains a JavasScript Array defintion contaioning the Object definition of the updated
* TreeNodes with its new attribute properties
*
* "added" contains a JavaScript Array of definition containing the Object definition of the treeNodes
* that should get Added to the Tree. Cause the Tree needs to know where to insert/append the TreeNode,
* the definition of the Node needs to contain an Attribute with the Id of its Parentnode "Parentnode"
* and Optional its Presilbing.
*
* A possible Response looks like this:

/**
* Load an {@link Ext.tree.TreeNode} from the URL specified in the constructor.
* This is called automatically when a node is expanded, but may be used to reload
* a node (or append new children if the {@link #clearOnLoad} option is false.)
*
* it now has the possibility to Update a node by requesting the data as an updaterequest
* @param {Ext.tree.TreeNode} node
* @param {Function} callback
* @param {boolean} update should the Request handled as an Update request?
*/
load : function(node, callback, upload){
if(upload === true) {
// call the Request Function with the upload flag == true
this.requestData(node, callback, upload);
} else {
// just call the normal load Function (it will then call the Request function)
Ext.ux.tree.SyncableTreeLoader.superclass.load.apply(this, arguments);
}

thank you very much for sharing this code. It is a very elegant solution, it looks great.

I have to test it for my use case. I use Ext.tree for the presentation and the operations on a structure tree. In addition I use the tree with Ext.tree.ColumnTree and Ext.tree.AsyncTreeNode.

I'm not satisfied with my first quick'n'dirty solution for the update logic. I hope you're okay I'm using your SyncableTreeLoader. I'll report of my tests.

My first impression is...

There are some changes to do to get this running with the backend I use. There is a rails app on the server and this provides a tree of an 'acts_as_nested_set' model. I hope someone understand what I mean.

The output of the method SyncableTreeLoader.getParams(...) should contain a timestamp of the last update. This means that the server response has to contain an update timestamp and that is stored in a member var in SyncableTreeLoader.processResponse(...).

I think that I will need this update timestamp on the server side to collect all changes since the last update. That seems to be easy.

Ok, we got deleted, updated and added nodes.

One more thing: Moved nodes.
After moving a node by another user the server response could contain this node as to be deleted and also as to be added at the new location. The rails app should store every move of nodes incl. a timestamp in a separate list, so the update response can be generated.
I realise that the deleted nodes should also be noted in a special list.

This is surely the first intuitive solution. Another solution could use the TreePanel method movenode : ( Tree tree, Node node, Node oldParent, Node newParent, Number index ).

Questions:

- You are yousing TreeNode.beginUpdate(), do you know, why this is not in the api docs? First I thought that was depricated, but the sources say its all right.

- How do you start an update?

Best regards

Alex

crp_spaeth

11 Feb 2009, 8:51 AM

Hi Alex,

In our environment there is no need for a "Moved" Object cause as you did expect we will add a moved Node to the deleted List and we will Add it to the Added List to...

For a Move Implemantation You should look up how a Move is Handled in Drag&Drop (I think this is the Fastest Way since Its written by the Ext Team :D )

I have no Use for that and got a lot things to do for now so I can't do that for you :(

About the other Question. I override the load method so it has an extra parameter (the third one) called update gives you the possibility to ask the server for an update response

see the updated Forms

alex-t.de

12 Feb 2009, 12:13 PM

Oh, I meant to post some extra code for the move operation if I find a nice solution for this.

And I've to ask one more time how the update mechanism is called. I try to explain how my first naive solution looks like. First there is a timer wich loads a list of node_IDs that has to be reloaded. Because the tree contains AsyncTreeNodes, this method is already usable. For example, if there is a Node '123' that has to be updated, I had to call the reload method of that parent node.

So is the new load method called for every node every n seconds? Or did I miss something?

Best regards

Alex

crp_spaeth

12 Feb 2009, 3:18 PM

Okay just to clarify the Load mechanism...

If you would like to use this update mecanism you will call the load-method with a third parameter: true

Since the update part of the load method will take care of all the Nodes of the response from the Server you will just need to call the load method on the Rootnode not for every Node.

// creation a task runner and run the task
var runner = new Ext.util.TaskRunner();
runner.start(task);

So whats happening here?

We have a Tree that uses our SyncedTreeLoader as its Loader.

Now we have the ability to call the load Method with an extra parameter (update Parameter) so that the same Request to the Server is made like if we called the normal load methode but with the small difference that in the Request will have an extra parameter in the request ( by default "&update=update" but configurable via updateParamName and updateParamValue)

So when we just have a normal call with an extra Parameter you will ask yourself where happens all the "Magic" :D:D:D

The SyncedLoader expects the in the documentation descriped data from the server now.

And Since The request has the update parameter in its Properties we can ask for that:

You can see for some of the attributes of the node there still needs do be some special handling done. So we apply all new Attributes to the old one (may we should add a flag to delete the old attributes first?) and after that we Update those special Attributes like the Text the icon and the tooltip.

I hope this will help you to get it work!

alex-t.de

14 Feb 2009, 5:50 AM

Thank you Martin!

I was unsure, but I guessed, that you have to run the update method on the root node. You've explained it well to me.

OT: Last night, I was thinking about my experiences with earlier projects. About 10 years ago I've coded a structure tree panel with html and php. In view of the usability it sucks. BTW javascript was out at that time. About 7 years ago I've coded a structure tree panel in Flash+XML+PHP. It was easy to use, easy to update, no extra code needed. But the rest of the webapp sucks, flash was not ready for the big apps. 6 years ago I've started with Java. The TableTree was a spasm, but finally it worked. Updating is still a problem. There are at least a handful Java bugs wich made the work on the tree very hard. If you're familiar with Java Trees or Swing, you know that you cannot fix some bugs at your own, because there are too many internal classes declared as private.
Anyway, I'm here now and I need a usable tree structure, wich is very reliable. And since I've switched to Macs I've thought about a column view like that one in the Finder. I've made a conception for a prototype in Ext JS and I'll try to do a nice extension. For now I can say that a lot of the operations a very easy to code. Updates are no big deal. The only unclear task is to manage the lists in a scrollable panel. But that should not be a big problem.
And I've some ideas to do the column view a bit more useful as a web structure tree panel. I think the finder does a great job with files and folders, but my web app has to do a great job on other demands.

So I've a lot of prototyping to do. Thanks for your help!

Best regards

Alex

hitekshu

22 Dec 2010, 1:53 AM

Hey crp_spaeth,

i also need a tree that is synced between 2 or more users, can you give me a complete example package with js and php code involved for this. Only if its not a trouble for you.