Posts Tagged "Flex"

Problem in downloading file

5.04.2010

POSTED IN Programming TAGS :

Click a button and download a file in Flex is very simple but Adobe’s official example has a problem which made me spent like 20 mins to find a work around.
This won’t work:

var request:URLRequest = new URLRequest("index.xml");
var fileRef:FileReference = new FileReference();
fileRef.download(request);

But this will work:

var fileRef:FileReference = new FileReference();
function downloadFile():void {   
fileRef.download(new URLRequest("index.xml"));
}
downloadFile();

Pass by Value

5.04.2010

POSTED IN Programming TAGS :

Actionscript 3 will always pass by reference unless the assignation is a primitive type (such as integer or string). I had an array and want to pass it by value (means it will copy the whole array’s value in it instead of referencing). It is because referencing will affect the referenced array when the new array is being changed in value.I came across with a script to deal with it. it may not be perfect, but have a look:

public function clone(source:Object):*{
  var myBA:ByteArray = new ByteArray();
  myBA.writeObject(source);
  myBA.position = 0;
  return(myBA.readObject());
}

Be aware that your objects in your array (or object)’s type may changed to Object/Array instead of it’s previous type.

UPDATE: There is also a method in Flex ObjectUtil class called “copy” ObjectUtil.copy(); which copies the specified Object and returns a reference to the copy.

Hopefully this is helpful to someone.

I had some troubles to re-load my itemrenderer. When the dataprovider of your collection (Tilebase or whatever) chage, the itemrenderer of exists items does not re-init, if your code is in creationComplete event. Finally I found a solution:in your itemrenderer file, on the creationComplete handler, add:

this.addEventListener(FlexEvent.DATA_CHANGE, dataChanged);dataChanged();

Then, in the dataChange function, you can do whatever you want for your itemrenderer when your dataprovider change.Hope this will help someone.

Currently available for freelance

Let me know if I could be in any help. Feel free to drop me an email: alan(at)hkalan.com or contact me