views:

496

answers:

3

I want to extend the FileReference class of Flex to contain a custom property. I want to do this because AS3 doesn't let me pass arguments to functions through event listeners, which makes me feel sad, so I need this property to exist on the event target, so I can access it.

I also want to be able to cast extant FileReference objects to this class without any fuss. I have:

var fr:SmxFR = e.target as SmxFR

and I want that to work; right now it just returns null.

A blank, newly instantiated SmxFR object has the extended property in place, but all of its inherited properties and objects return Error: Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.

This is the class I am using, SmxFR.as:

package
{
import flash.net.FileReference;

public class SmxFR extends FileReference
{
 public var housenum:String = "";
 public function SmxFR()
 {
  super();
 }  
}
}

Kept it as straightforward as I could, really. Can someone please help me figure this out? Thanks.


Edit:

Per request, this is the instantiation which results in the aforementioned error in all inherited objects:

var fr:SmxFR = new SmxFR();

I get living handle property from that, and all other (that is, inherited) properties throw Error #2037.


So, maybe what I want to do is going to require overriding FileReferenceList? If the original objects must be instantiated to SxmFR, that's what I'll have to do, since I'm using FRL to allow the user to select multiple files at once. Are you guys sure there is no way to fast from a FileReference to my class?

A: 

Extending FileReferenceList is not going to be helpful. FileReferenceList.browse() method creates an array of FileReference object when user selects multiple files - that happens internally (may be in its private methods) and you cannot change that behavior and force it to create SxmFR objects instead. Use custom events as Myk suggested.


This article talks about Sound objects, but may be that's applicable to FileReference objects too. May be you cannot reuse them. Post the code where you use the SmxFr class and get the said error.

Amarghosh
I've updated the question with a response. Thanks.
cookiecaper
A: 

When e.target is instantiate as FileReference you can't cast it to SmxFR because it's not in the line of inheritance. In the other way you can a SmxFR Object to FileRefernce.

Jochen Hilgers
Hmm, I am using a FileReferenceList to allow the user to select multiple files at once. I suppose I will have to override that to bear forth `SmxFR` objects instead of `FileReference` objects in `FileReferenceList.fileList`?
cookiecaper
Yeah, your best bet is to extend event to do what you want. See my suggestion for a writeup. Move the car, not the road. ;)
Myk
+2  A: 

You can totally pass objects via event listeners, it's just done in a specific way. I'd learn to do it correctly, rather than trying to extend a core library which could cause you problems later if you make a small mistake.

My solution: instead of extending FileReference, extend Event and add your properties to that.

var myEvent:MyExtendedEvent = new MyExtendedEvent();
myEvent.myCustomProperty = myValue;
dispatchEvent(myEvent);

Then in your handler you just write:

function myEventHandler(e:MyExtendedEvent):void {
     trace(e.myCustomProperty);
}

Much more painless to go down this road! The added benefit is that if any other Flash Developer anywhere ever looks at your code they're not going to get hit in the face with a non-standard customized FileReference. :)

Myk
I tried this, but the original listener is on Event.COMPLETE. I want custom events to fire when that gets sent. As such, I must dispatchEvent when Event.COMPLETE fires for that FileReference ... the problem, then, is that even if my response for fr.Event.COMPLETE is to dispatch an event ... I still don't have the parameter I want to pass. Do you understand or know how to help? I really need to get this done.
cookiecaper
Your complete handler should be able to help you out - you know that you can refer to Event.currentTarget, right? The event carries a reference to the object that dispatched it. So if you want access to property myVal and it's a property of the dispatching object, you can do something like this:function completeHandler(e:Event):void {var myVal:Object = e.currentTarget.valueYouWant;var myEvent:MyCustomEvent = new MyCustomEvent();myEvent.myVal = myVal;dispatchEvent(myEvent);}Does that make sense? Spend some time really getting to understand the Event model, you will learn a lot.
Myk