Real World Flex/ColdFusion: Part 5

Word Count: 281

FI know this series seems like its going to go forever so please let me know if you want me to start summarizing a little more. I just felt like the more detail the better. In this part of the series we are really going to start diving into the meat of the application. If you remember from the 1st part of this series I had a very clear direction I wanted to take with the directory structure. There can be an infinite number of sub folders so loading them all at once is not the answer for me. Trying to make this a good experience for the end user I want to load the root directories first and then as requested load sub directories.

So first order of business is to setup a ColdFusion component that can handle the directory listings. This part actually took me a little while to get through because I was set on using an all CF solution at first. There is no easy way to get a directory only listing. When you use cfdirectory and give it a path you will get a listing of both files and folders. You could then do a query of queries on that listing and find only the directories which is what I tried in the beginning only to find out that speed was a major issue here. After some testing I realized it was back to the drawing board. After an article I found on Brian Rinaldi's blog I realized that tapping into Java was going to be the best solution for the job.

Go ahead and create a FileManager.cfc component under org/vega/ and use the following code. Lets try and walk through this code. Not only will we use this code when grab the top level but we will use it when we are grabbing sub directories for a folder so it needs to be able to handle both. The path is a required argument and after that the other 2 are not. This way our first call will have to be for the root directories but every subsequent call can just be getDirectories("path"). The rest is pretty simple once you check out the Java.io.File API. The important thing to not here though is the children array. The way the Flex tree works is pretty cool and I found a cool way to "trick" it if you will. If the tree see's a children array (event an empty one) it knows that this is a directory that can be expanded. Therefore any folder that has children will have the plus next to it so you can expand it and anyone without children will just be a folder letting the user know that this folder has no children.

Now that we have that out of the way we are ready to get to work. Here we are doing a couple things, first setting up our remote object so that we can talk to our FileManager component. We have a result and fault event handlers set. Right now the list directories method does not do anything but we will get to it. The fault event handler will simply trace out the error message if something happens. After this is done if your not seeing the directories that is the first place you will want to look to find out the cause of the problem. So our component is going to return an Array and the conversion on the back end works out good for us so our directories variable is an array. It needs to be set to bindable so that our tree can use it. Everything else here should be self explanatorily, if it is not just let me know.

Finally we need to fill in our list directories method. We know the event.result is going to be an array because our component is returning one so we need to cast it here to ensure the compiler that this is what is coming back. If the directories variable is null then we know we have not loaded any directories yet. As you can see this will come in handy later because we will know the root directories are there so we must be loading children of some directory.

That's all there is to it, if your following along at home you should have something that looks like this. You will notice that a directory with a plus next to it indicates to the user that it has sub directories. In the next installment we will take a look at how to load the sub directories when they are requested as well as setting up our data grid to load the files of a selected directory.

Comments

#1 Posted By: Steve W Posted On: 1/7/09 2:19 AM
Dan, I think your are taking us along at the correct pace. People might be able to handle a faster pace, but like you said, the more detail the better. Besides, it's your blog and you can do whatever you want.
#2 Posted By: Chris Posted On: 1/7/09 9:16 AM
Dan, this is great. Great pace and content! I have been waiting for something like this to see how other's code in Flex. Thanks again!
#3 Posted By: Gareth Arch Posted On: 1/7/09 12:33 PM
Another vote for keeping the pacing as it is. Better to go a little slower for those who may need the details most. Plus there may be things that even those experienced coders might miss if some of the details were glossed over.
#4 Posted By: johans Posted On: 1/13/09 7:21 PM
In CF8 you can easily get a directory only listing with cfdirectory by using the type="dir|file|all" attribute with value = dir.

I have not compared performance with native java methods however.
#5 Posted By: johans Posted On: 1/13/09 7:28 PM
Also - use recurse="yes" for cfdirectory to return all subdirs. Recurse was added in CF7.
#6 Posted By: Dan Vega Posted On: 1/13/09 7:37 PM |
Author Comment
I knew that going in but I really wanted to keep it compatible with 7. Also I don't want to recurse down the directory tree because speed can be a major factor if there are tons of directories. That is why I like the approach of loading the base directories and getting what I need when they are requested.

You are more than welcome to download the code and update so it uses CF8. Thanks for reading!
#7 Posted By: Rocheking Posted On: 3/5/09 8:22 PM
This is a great series in all the ways mentioned already. I would just add a reminder to continue to escape the \s if you set a deeper _baseDir.
#8 Posted By: Dan Vega Posted On: 3/5/09 8:24 PM |
Author Comment
Thank you Rocheking! Great point on the escape as well..


Post Your Comment







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.