Script 9.4 in “Adobe AIR with Ajax: Visual QuickPro Guide”

January 1, 2009

Someone pointed out in a forum posting that there was a problem with Script 9.4 of my Adobe AIR with Ajax: Visual QuickPro Guide (View at Peachpit.com). Script 9.4 lists the items in a directory (a folder on the user’s computer). By clicking on the name of any item, that item will be moved to the user’s trash. Then the script removes and rebuilds the list of items. I’m not sure if this is due to a change in AIR 1.5 or not, but the problem is that after deleting an item and the list is rebuilt, clicking on any other item no longer works. The solution I came up with may actually be better than the original script (in fact, it probably is). The solution is to remove the specific item that was deleted from the list, instead of deleting and rebuilding the entire list. To fix this script, change the makeList() function so that it assigns a unique ID to each list item. You could accomplish this by creating a counter and using that value for each item. Instead, I just used the item’s nativePath value as its ID (since we’re already using that anyway and it must be unique). So in the makeList() function, after the current setAttribute() line, I added:

li.setAttribute('id', contents.nativePath);

Then in the deleteItem() function, take out the code that removes the list and calls the makeList() function. Insert this:

document.getElementById('contents').removeChild(document.getElementById(which));

That says, “Find the element with an ID of ‘contents’–i.e., the list–and remove the child of that element whose ID value is the value of the ‘which’ variable.” That variable is assigned a value when this function is called, that value being the nativePath of the item being deleted.

I hope this helps anyone confused by the problem and let me know if you have any questions about this.