Can an ActionScript component listen on its own events? Event name?

I have a CircleButton class in ActionScript. I want to know when someone from outside changed the 'on' property. I am trying to listen to "onChange", but it never removes this event handler.

I know that I can write the 'on' property as get / setter, but I like the ease of use [Bindable]

Can an object not listen to its own events?

public class CircleButton extends UIComponent { [Bindable] public var on:Boolean; public function CircleButton() { this.width = 20; this.height = 20; graphics.beginFill(0xff6600, 1); graphics.drawCircle(width/2, height/2, width/2); graphics.endFill(); this.addEventListener(MouseEvent.ROLL_OVER, rollover); this.addEventListener(MouseEvent.ROLL_OUT, rollout); this.addEventListener('onChange', onOnChange); } private function onOnChange(event:PropertyChangeEvent):void { 
+2
source share
4 answers

If you use the [Bindable] tag without specifying the type of event, then when the property changes its value, an event of the type: PropertyChangeEvent.PROPERTY_CHANGE, which is the string 'PropertyChange', will be sent.

Therefore, in order to be able to register to listen to this event, you need to say:

 this.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onOnChange); 

The reason your listener function has never been called is because the type of event was wrong.

Note that the listener method will be called if any of the variables marked as Bindable in your class change, not just 'on'. This event has a property β€œproperty” that indicates which variable has been changed.

To avoid calling each Bindable variable, you need to specify the event in the [Bindable] tag:

 [Bindable(event="myOnChangeEvent")] 

and send this event manually, given that the property is changing (i.e.: in the installer), although this is not like what you wanted to do.

+7
source

You can use BindingUtils.bindSetter ()

An example is found here .

+1
source

Just because it’s possible to associate something with a variable does not mean that something is associated with a variable. This is a bit like an event system - just because something can send an event does not mean that it is listening.

The classes around which Flex bindings are based are BindingUtils and ChangeWatcher. When you directly bind to MXML (which is simply converted to AS3 by the compiler), it uses these classes behind the scenes to actually set the binding. I dug into ChangeWatcher before and when it looks at a list of potentially related objects, it only sends messages if any object is actually listed as a listener. The whole binding system is a really smart shell around the event system.

The binding semantics in AS3 instead of MXML are different. Subtle differences (like binding to child properties of objects) that just work in MXML require work in AS3 to duplicate the behavior (probably the result of code generation between MXML and AS3).

Check out the Adobe doc for a little info on ChangeWatcher in AS code.

Personally - I do not use binding outside of MXML, as I feel that this is awkward. I would advise you to write a setter function, since it is more predictable (and most likely is efficient). I also suggest you read the source code for ChangeWatcher and BindingUtils. These are definitely some of the most advanced AS3s you are likely to read.

+1
source

One of my favorite approaches is the Observe class, which is here here . This is essentially using a setter, but it is a good repeatable approach.

+1
source

Source: https://habr.com/ru/post/1301495/


All Articles