PDA

View Full Version : Wait for the user to save a file (xml/datagrid/file saving)



coggia
December 11th, 2009, 02:05 PM
Hello,
I got a little issue and i can't think of a nice way to fix it :

To quickly explain things:
step1- I got an xml that is locally loaded(works fine)
step2- the content goes into a datagrid that is displayed (works fine)
step3- In AS3 I dynamically add some lines to this xml file(works fine)
step4- I save it locally (works fine)
step5-and want the datagrid to be updated..(works not-so-well)

So, in step 4, the data is saved into the xml file, but in order to finish the save, the user has to click on the "Save as" windows in order to have the file updated of course.. and that's where's my problem, how can i tell flash to wait till the user actually saves the file, before reload the xml file in the datagrid ? here's the code :



//(STEP1 & STEP 2) BEFORE THAT

//called when the user click to add the new line in the xml file
function add_button_clicked(event:MouseEvent) {
//here the new line inserted in xml (STEP 3)
var node:XML;
node = <fact fourn={nom.text} />;
fileXML.appendChild(node);

//here the code to save it to the disk (STEP4)
var ba:ByteArray = new ByteArray();
ba.writeUTFBytes(fileXML);
var fr:FileReference = new FileReference();
//HERE a window will be logically displayed with "do you want to write onto file.xml ?"
fr.save(ba, "file.xml");

//here code to load the xml file and display it in the datagrid (STEP5)
//but this is executed BEFORE the user clcik on the rewrite window
loadCat("file.xml");
}
So we see that in my step 5, i need to wait that the user actually click yes before loadCat(file.xml) is executed..and I don't know how..

Krilnon
December 11th, 2009, 02:45 PM
If you scroll down to the 'Events' portion of the method description for FileReference#save, you'll notice that there are a number of events that your class can listen for. I think that 'select' is dispatched when they pick a file to save to.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html#save()

It's probably not the best idea to load 'file.xml' after you've just saved it, I mean, you already know what's going to be in the file, right? You might as well just dump the ByteArray's contents into a string and then parse that as XML.

coggia
December 12th, 2009, 02:53 AM
It's probably not the best idea to load 'file.xml' after you've just saved it, I mean, you already know what's going to be in the file, right? You might as well just dump the ByteArray's contents into a string and then parse that as XML.

Mmmmhhh..Yes, you're right , I'll work on that way, it's more logical :)

I guess Ihave to stop trying coding at late hours, nothing good comes out of my mind. ;)

Thanks again .

Krilnon
December 12th, 2009, 02:55 AM
Don't worry about it. I just realized that you also already have the XML object from when you created it earlier, so it would make even more sense to use that than use the ByteArray like I suggested. :trout: