Hi,

I have implemented a DynamicTree for my own usage, after spending
almost a week looking for an URL about this subject.
Until now, I haven't seen any implementation, and I plan to write a
pot in my blog about it.

The secret is one event called itemOpening of the tree... there's no
need to hack getChildren.

Th second point, as much important as the first one are the requests
persistence.
When you fire an event using HTTPService and you call another request
without completing the first one, you'll have troubles, for sure.
I overcome this issue by using token (httpServiceInstance.send(params)
returns an AsyncToken instance), which provided me the ability to
receive all instances. without using this (and defining my custom
params again), the request first was never completed and second never
has the correct values.
Ok, then I have the requests queue... how do I remember which node I
was looking for? I had to include another queue to control that.
Then, it started another issue... how will I know that request 1 is
for node 1 and node for node 2 (which has a faster response)? Again,
another control to that.

The third point is to decide which item has children or not. It's
defined by a property isBranch.


After all issues controlled, we can come with the implementation...

You start the tree with the rootElement. Consider passing the rootId.
You can implement it without root, but you'll have to modify my
implementation... =)

So, your tree node will be something like this:

<?xml ...?>
<root>
  <item id="1" name="Foo" isBranch="false"/>
  <item id="12" name="Bar" isBranch="true"/>
</root>

Then you implement the node children retrieval in the event itemOpening.
In my source, I added a dummy node "Loading..." and then removed it
when it loads the things.

Here's my full class, based on my implementation and without any line
of code removed.
I hope it helps:


<?xml version="1.0" encoding="utf-8"?>
<mx:Tree xmlns:mx="http://www.adobe.com/2006/mxml"; showRoot="false"
labelField="@name"
                dataProvider="{treeDataProvider}" 
itemOpening="handleItemOpening(event)">
        <mx:Script>
                <![CDATA[
                
                        import mx.controls.Alert;
                        
                        import mx.rpc.AsyncToken;
                        import mx.collections.ArrayCollection;
                        
                        import mx.rpc.events.ResultEvent;
                        import mx.rpc.events.FaultEvent;                        
                        import mx.events.TreeEvent;
                        
                        
                        protected var _url:String;
                        
                        public function set url(url:String):void
                        {
                                this._url = url;
                                
                                itemService.url = url;
                        }
                        
                        public function get url():String
                        {
                                return this._url;
                        }
                                                
                
                        protected var _rootId:uint;
                        
                        public function get rootId():uint
                        {
                                return this._rootId;
                        }
        
                        
                        protected var itemOpeningArray:ArrayCollection = new 
ArrayCollection();
                        
                        
                        [Bindable]
                        protected var treeDataProvider:XMLList;
                        
                        
                        public function setTree(rootId:uintg):void
                        {
                                this._rootId = rootId;

                                sendRequest(this._rootId);
                        }
                        
                        
                        protected function 
handleItemOpening(event:TreeEvent):void
                        {
                                // Creating loading item
                                var loading:XML = <item name="Loading..." 
isBranch="false"/>;
                                XML(event.item).setChildren(loading);
                                        
                                // Storing item reference       
                                itemOpeningArray.addItem(event.item);
                                                
                                // Dispatching children event
                                sendRequest([EMAIL PROTECTED]);
                        }
                        
                        
                        protected function 
handleItemResult(event:ResultEvent):void
                        {
                                // Formatting result list
                                var treeXMLList:XMLList = 
event.token.result.data.item as XMLList;

                                // Check if we are attaching children to root 
or an item                                
                                if (itemOpeningArray.length > 0)
                                {
                                        // We need to pick the right item now
                                        for (var i:int = 0, len:uint = 
itemOpeningArray.length; i < len; i++)
                                        {                                       
        
                                                if 
(XML(itemOpeningArray[i])[EMAIL PROTECTED] == event.token.params.id)
                                                {
                                                        var item:XML = 
itemOpeningArray[i];
                                                        
itemOpeningArray.removeItemAt(i);
                                                        
                                                        break;
                                                }
                                        }
                                                                        
                                        item.setChildren(treeXMLList);
                                }
                                else
                                {
                                        treeDataProvider = treeXMLList;
                                }
                                
                                
                        }
                        
                        
                        protected function 
handleItemFault(event:FaultEvent):void
                        {
                                Alert.show(event.fault.toString(), "Error 
processing request");
                        }
                        
                        
                        protected function sendRequest(id:uint):void
                        {
                                // Create POST params
                                var params:Object = new Object();
                                params.id = id;
                                
                                // Dispathing request
                                var token:AsyncToken = itemService.send(params);
                                token.params = params;
                        }
                        
                ]]>
        </mx:Script>
        
        <mx:HTTPService id="itemService" method="POST" useProxy="false"
resultFormat="e4x"
                        result="handleItemResult(event)" 
fault="handleItemFault(event)"/>
</mx:Tree>



If you need more help, just ask.


Cheers,


On Mon, Aug 4, 2008 at 6:24 AM, Johannes Nel <[EMAIL PROTECTED]> wrote:
> The class you need to look at to load sub nodes on demand is
> the ITreeDataDescriptor and the method you ned to implement is public
> function getChildren(node:Object, model:Object=null):ICollectionView
>
> On Sun, Aug 3, 2008 at 9:50 AM, profiles_arun <[EMAIL PROTECTED]>
> wrote:
>>
>> Hi Jon,
>>
>> Thanks for you response, but basically i have the requirement to
>> load the complete tree.
>> Since am new, can you give me a snippet of code, which will load the
>> sub-nodes of the tree on demand ?
>>
>> Thanks in advance.
>> arun
>>
>> --- In flexcoders@yahoogroups.com, Jon Bradley <[EMAIL PROTECTED]> wrote:
>> >
>> >
>> > On Aug 2, 2008, at 7:25 AM, profiles_arun wrote:
>> >
>> > > I have a tree, which has 21000 plus nodes, when i load the .swf
>> file
>> > > in IE, it crashes. As such i dont get any error messages, so i
>> > > checked into widows application error log and found this error
>> > >
>> > > "Faulting application iexplore.exe, version 7.0.6000.16674,
>> faulting
>> > > module fldbg9f.ocx, version 9.0.124.0, fault address 0x00241305."
>> >
>> > Your first problem is how much data you are loading into a tree. In
>> my
>> > opinion, there is no reason to be doing that.
>> >
>> > Second problem, IE 7. It's the absolute worst browser on the
>> planet
>> > with CPU and memory management. Try this in Firefox. Although you
>> > might crash, it will probably take longer to crash (unless there's
>> a
>> > bug in the debug flash player).
>> >
>> > > Below is the snippet of my impl. Please suggest a solution, to
>> > > overcome this issue.
>> >
>> > Your solution to overcome the issue is as you already found -
>> don't
>> > try to load that much information into a Tree.
>> >
>> > cheers,
>> >
>> > jon
>> >
>>
>
>
>
> --
> j:pn
> \\no comment
> 



-- 
Guilherme Blanco - Web Developer
CBC - Certified Bindows Consultant
Cell Phone: +55 (16) 9166-6902
MSN: [EMAIL PROTECTED]
URL: http://blog.bisna.com
Rio de Janeiro - RJ/Brazil

Reply via email to