flex: How to respond to change of data inside a component -


i've created custom component based on image component. want teach respond change of binded variable. e.g. if main class has variable balance, want component change image in case balance = 100 1 image, in case balance = 50 another.

can understand how that


i using flex3 don't see propertywatcner there. using component2 main mxml file way

<mycomp:myicon left="15" top="20" width="60" height="60"     id="tower" price="100" accountstate="{accountmoney}"     click="drawbuildingshadow(event)" /> 

and inside component myicon want able react changes of binded accountstate variable.

without code go (please include sample of both components if can), there number of ways approach this.

you add changewatcher bound variable in question, , invokes method when property changes. method change image, based on whatever conditions apply property.

it something this:

component 1:   [bindable]   public var yourvariable:number;  component 2:   private var propertywatcher:changewatcher;   //in initialization method -- example method how create property watcher   public function init(): void {       ...       propertywatcher = changewatcher.watch(component1, "yourvariable", onvariableupdate);   }    //define new method handle when property changes   private function onvariableupdate(event:propertychangeevent):void {      if(event.newvalue == 50) {        yourimage.source = newsource;      }      else if(event.newvalue == 100) {        yourimage.source = othersource;      }   } 

obviously, very truncated , shorthand, started.


edit: changewatchers exist in flex 3, sounds should go in different direction. since code snippet posted bit small, i'm going make few assumptions on how might :)

as alxx mentioned in comment, can change property accountstate in component actual property, setter/getter. allow more extensive processing when accountstate gets updated.

it should something this:

mycomp:

//inside script tag: private var _accountstate:number;  [bindable] public function accountstate():number {     return _accountstate; } public function set accountstate(state:number):void {     _accountstate = state;     switch(state) {         case 50:             youricon.source = "blahblahblah";             break;         case 100:             youricon.source = "blahblahblah2";             break;         //and on     } } 

this won't change code posted: should still work you've written it. haven't tested this, it's possible i'm missing something, :)


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -