I like to think of the watch like a filter. I first started using watch
because you can get immediate notification of a change and then run an event
because of that change. But because you have to return the value in order
for it to get set, it makes more sense to use a getter/setter if you want to
run events after the property has been changed or accessed.

I have used watch to run functions and pass arguments via a string set by
javascript. In that case, saving the string wasn't as important as receiving
the immediate event and parsing the function and arguments. 

swf.SetVariable("watchVar", args )

As for the difference between watch vs. listener/broadcaster, it's a lot
like comparing apples and oranges. 


//
//      watch
//


var width = 0;
var height = 0;
var max = 100;

watch ( 'width', validateNumber, max );
watch ( 'height', validateNumber, max );

function validateNumber ( prop:String, o, n, extra ):Number
{
        if (isNaN(n)) { return o } else { return Math.min(extra,n) };
}

function area():Number
{
        return width * height;
}

width = 50;
height = 2;
trace( area() ); // 100

width = 100;
trace( area() ); // 200

width = 200;
trace( area() ); // 200

height = 'random string';
trace( area() ); // 200



//
//      getter / setter
//



__width = 0;
__height = 0;
__area = 0;

addProperty( 'width', get_width, set_width );
addProperty( 'height', get_height, set_height );
addProperty( 'area', get_area, null );

function get_height():Number{return __height }
function get_width():Number{return __width }
function get_area():Number{return __area }

function set_height( val:Number ):Void
{
        if ( isNaN(val)) return;
        __height = val;
        calculateArea();
}
function set_width( val:Number ):Void
{
        if ( isNaN(val)) return;
        __width = val;
        calculateArea();
}

function calculateArea ():Void 
{
        __area = __height * __width;
}

width = 2;
height = 10;
trace ( area ) // 20


_____________________________

Jesse Graupmann
www.jessegraupmann.com   
www.justgooddesign.com/blog/    
_____________________________



-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
[EMAIL PROTECTED]
Sent: Saturday, August 11, 2007 12:06 PM
To: flashcoders
Subject: [Flashcoders] Q:Watch vs listener/broadcaster

Hi
I'm aware of watch, but have never really used it.
I was wondering if perhaps I am missing out on a really useful tool.

Can someone  give me an example of when it would make sense to use watch
instead of setting up a listener broadcaster?

[e] jbach at bitstream.ca
[c] 416.668.0034
[w] www.bitstream.ca
--------------------------------------------
"...all improvisation is life in search of a style."
             - Bruce Mau,'LifeStyle'
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to