Hey,
Vaan S Lanko wrote:
> Does anyone know whether there is an event fired when an object is
> instantiated ? I have objects that are created from an sqlite SQLEvent
> result. Presently I'm iterating over the results and calling the
> populateBar function manually, but it would be nice for the object to
> detect its been created and dispatch an event automagically.
>
> The Event.ACTIVATE event sorta works but only if u move focus out of the
> AIR app and then back to it again. This is the code i have been working
> with sofar....
>
> package
> {
> import flash.events.Event;
> import flash.events.EventDispatcher;
> [Bindable]
> public class Foo extends EventDispatcher
> {
> public function Foo()
> {
> super();
> this.addEventListener(Event.ACTIVATE,fooLoadedHandler);
> }
> public function fooLoadedHandler(event:Event):void
> {
> if(this.bar==null) this.populateBar();
> this.removeEventListener(Event.ACTIVATE,fooLoadedHandler);
> }
> public var bar:Bar;
> private function populateBar():void{
> //.. this.bar = stuff
> }
> }
> }
>
> any comments would be greatly appreciated, maybe even an alternative
> strategy..
>
I assume the problem you have is that not all the properties have been
set on the Object inside the constructor so you can't perform any
additional related object construction(ie) fetching another related
object from the DB hence wanting an event (FWIW there is not one).
If that is the case you can prob do somethign like:
private _barForeignKey:Number; //FK to Bar DB object
private var _bar:Bar; //Bar DB Object ref
public function set barForeignKey(id:Number):void{
if ( _barForeignKey != id) {
_barForeignKey = id;
dispatchEvent( new Event("Foo.BarFKSetEvent") );
}
}
[Bindable event(name="Foo.BarFKSetEvent" type="....Event")]
public funciton get bar():Bar{
_bar = fetchBarFromDB(_barForeignKey);
return _bar;
}
Or you can attach the listener and handler for the event
Foo.BarFKSetEvent in the constructor like you have in your example.
Perhaps, and this is a total guess, if you had a constructor that
matched the values for the SQL select it would use that constuctor and
your properties would be available in the constructor. However that is
a wild guess on my part and probably not the case as it would make
object construction from the SQL event more complex. Might be worth
trying though.
I assume it actully just does somethign like:
f = new Foo();
f.setBarForiegnKey(new Number(10)); // or whatever.
HTH.
shaun