简体   繁体   中英

Flex: Actionscript & DataBinding using XML File

I've created my own component, that uses a XML File's lastResult to bind to it's dataProvider property.

The binding happens in AS as follows: BindingUtils.bindProperty(this.myChildComponent1,"dataProvider",this,"dataProvider");

The above successully binds the this.dataProvider to my child components dataProvider . The problem arises that I have another component that I need to bind values of a child of the result XML. More so like:
this.dataProvider.child('NextNode')

But the issue is that when the component's createChildren() gets called the XML lastResult isn't populated yet as the request is a asynchronous HTTPRequest hence this.dataProvider equals null .

Eventually when the data arrives it's correctly binded internally, but I can't bind the data's next child to another component ( this.myChildComponent2 ), either by :
BindingUtils.bindProperty(this.myChildComponent2,"dataProvider",this,"dataProvider.child('NextNode'"); : says no such child.

or by

BindingUtils.bindProperty(this.myChildComponent2,"dataProvider",this,this.dataProvider.child('NextNode')); : says this.dataProvider is null/undefined.

Even issuing: if(this.dataProvider == null) {this.invalidateProperties();} else //Bind Child does not work as commitProperties is getting called only once inspite of me calling invalidateProperties() after checking inside commitProperties() .

Any help is appreciated. Thanks.

I haven't tested this out, nor have I tried binding to XML & XML children before, but here goes.

You should try to use a property chain in order to do this binding. Your code doesn't look like it should work at all since the BindingUtil using strings as property descriptions rather than a string that represents a function you want to call. Your code: .

BindingUtils.bindProperty(this.myChildComponent2,"dataProvider",this,"dataProvider.child('NextNode')");

Should instead be: .

BindingUtils.bindProperty(this.myChildComponent2,"dataProvider",this,["dataProvider","NextNode"]);

Besides your code looking like it shouldn't work, there are other reasons to do it this way.

  1. XML children will do not dispatch propertychange events, so if the child node did change, it wouldn't update in your second component anyways.

  2. Using the chain of property's means that changes to the "dataProvider" AND "NextNode" will be looked for, so if either change the binding fires.

  3. Using the chain means that you don't have to worry about the XML not being loaded at startup. If a property in the chain isn't available then the target is just set to null.

So, if you use this method, only changes to the "dataProvider" are going to trigger the binding (this.dataProver = something else or the like). If you want to have the binding also get triggered via a change to the 'NextNode' you will have to manually dispatch a PropertyChange event to indicate that a change happened: .

dataProvider.dispatchEvent(new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE,false,false,PropertyChangeEventKind.UPDATE,"NextNode",*oldValue*,*newValue*);

Where the oldValue/newValue are optional. Let me know if this works... I think it should but hey, who knows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM