开发者

ChangeWatcher not firing on bound property change

On creation complete my parent component executes an "init" function which simply sets public bindable variables in child components. I'd like the child components to watch these variables and upon being set use them. However, for some reason the ChangeWatcher is not firing with the change of the variable. Below is my code; anyone see what's wrong? Thanks.

Parent Component

private function init():void
开发者_StackOverflow{
 userInfo.user = new User("Tom");
}

Child Component -- "userInfo"

...
[Bindable] public var user:Object;

private function init():void
{
  ChangeWatcher.watch(this, "user", function(evt:PropertyChangeEvent):void
  {
    Alert.show(evt.property);
  }
}


You said:

On creation complete my parent component executes an "init" function which simply sets public bindable variables in child components. I'd like the child components to watch these variables and upon being set use them.

It breaks encapsulation to try to have the child component access the parent. Parents can access, children, but children should never access parents. However, any given component should be aware of when it's values changes and update itself accordingly. The use of get/set properties and the Flex Component LifeCycle are the proper way to do that.

When you make a component Bindable that means it can be used as the source of data binding. It almost sounds to me like you're trying to use said properties as the target for data binding and are struggling to update your component.

If I understand what you're after, I wouldn't use the ChangeWatcher at all. Just expand your user property into a get/set property and either implement your changes in the set method of the property, or possibly in commitProperties using a flag.

Something like this:

private var _user : Object;
[Bindable]
public function get user():Object{
 return this._user;
}

public function set user(value:Object):void{
 this._user = value;
 Alert.show(value);
}

Or even better:

protected var userChanged : Boolean = false;
private var _user : Object;
[Bindable]
public function get user():Object{
 return this._user;
}
public function set user(value:Object):void{
 this._user = value;
 this.inalidateProperties()
 userChanged = true;
}

public function commitProperties():void{
 if(userChanged == true){
   Alert.show(value);
   userChanged = false;
 }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜