Skip to main content
Apr 24, 2012 3:28 PM UTC
Question

Analog of Watch() in AS3

Hello everyone!

Here is a situation:

var MAIN_OBJ:Object = new Object();

MAIN_OBJ.array_1 = new Array(1, 2, 3);

MAIN_OBJ.array_2 = new Array(A, B, C, D, E, [f,g,h,i,j]);

MAIN_OBJ.array_3 = new Array([k,l,m,n,o,p], Q, R, S, T);

ActionScript2:

function someFunction(prop, oldVal, newVal, limit) { trace("prop is about to change"); return newVal;}

MAIN_OBJ.watch("array_1", someFunction);

ActionScript3:

Use accessor properties (get/set functions) or the flash.utils.Proxy class for similar functionality.

Here is the task:

Watch some property of object to change, and do smth. Watch not the whole object, only one property.

I know that ExternalInterface.addCallback provides smth like that, but for the whole object.

I've never used get/set functions or override smth, please explain what should exactly be written to achieve the same effect as in AS2. The simplier it would be - the better.

Sorry fo my English.

This topic is closed to new replies. Start a new post to keep the conversation going.

18 replies

Participating Frequently
Sep 23, 2022 10:02 AM UTC

I meet ths same situation,something like javascript's Object.defineProperty.I just want it dispatch auto,so that I can update my pane

Translate
kglad
Community Expert
Community Expert
Sep 23, 2022 4:23 PM UTC

if you want to dispatch a js event, use:

 

whatever_element.dispatchEvent(new Event("whatever_event"));

Translate
Participating Frequently
Sep 25, 2022 2:17 AM UTC

In fact,I'm going to make my panel update when an object's specify property change.Yes,I can bind a listener and dispacth it after this object's specify change,but the question is I don't want to do the dispatch step,like hide it in somewhere.In ES5 method Object.defineObjectProperty() can help me do this,it use getter() and setter() do something more when  an object' property change,but not a proxy.

So I'm wondering whether if this is a way can achieve some like this in AS3.

Translate
kglad
Community Expert
Community Expert
Apr 25, 2012 1:34 PM UTC

// in watching class:

watchedClass.addEventListener("propertyChangedE",propertyChangedF);

private function propertyChangedF(e:Event):void{

// do whatever

}

// in watched class, dispatch event whenever changed from inside the class and from outside:

public function set someProperty(someValue:ValueType):void{

_someProperty=someValue;

dispatchEvent(new Event("propertyChangedE"));

}

Translate
Apr 26, 2012 4:43 AM UTC

I suppose that to change someProperty, I should use

setProperty ( target, someProperty , value);

I'd better add more details to the question:

var MAIN_OBJ:Object = new Object();

MAIN_OBJ.array_1 = new Array(1, 2, 3);

MAIN_OBJ.array_2 = new Array(A, B, C, D, E, [f,g,h,i,j]);

MAIN_OBJ.array_3 = new Array([k,l,m,n,o,p], Q, R, S, T);

MAIN_OBJ.array_1.push(4);

MAIN_OBJ.array_1.splice(1,1);

I think, in this case it is not possible to use setProperty 

Translate
kglad
Community Expert
Community Expert
Apr 26, 2012 5:44 AM UTC

again, // in watched class, dispatch event whenever changed from inside the class and from outside:

Translate