开发者

flex: using arraycollection as bind variable gives empty data

i would be glad if anyone can answer this.

i have an actionscript class as follows:

    package
    {
        import mx.collections.ArrayCollection;
        import mx.controls.Alert;
        import mx.core.UIComponent;

        public class GridComponent extends UIComponent
        {

            private var _array:ArrayCollection;

            public function GridComponent()
            {
            }

            public function get array():ArrayCollection
            {
                return _array;
            }

            public function set array(value:ArrayCollection):void
            {
                _array = value;
                Alert.show(value.length + "");
            }

        }
    }

i invoke this from a mxml file; array variable is a array collection which is pre-populated with some data.

private var array:ArrayCollection = new ArrayCollection();

                            var person1:Person = new Person();
                person1.id = 1;
                person1.name = "pavzie1";

                var person2:Person = new Person();
                person2.id = 2;
                person2.name = "pavzie2";

                var person3:Person = new Person();
                person3.id = 3;开发者_如何学运维
                person3.name = "pavzie3";   

                                array.addItem(person1);
                array.addItem(person2);
                array.addItem(person3);     

    <local:GridComponent array="{array}">

    </local:GridComponent>

Person is a pojo class with 2 instance variables id and name

the result is i get a length of 0. what could be wrong?? Also if i put a number instance variable with setters and getters , it binds the value correctly, so problem is only with binding of an array collection.


There is a logical reason this is happening. First, you are calling private var array:ArrayCollection = new ArrayCollection(). This means that this ArrayCollection gets created when your MXML file is instantiated. At this point, you have an empty ArrayCollection. Next, when your component is setup, it has the value of that empty array collection passed to the setter (which at this point is an empty ArrayCollection).

At this point, all is working as planned. However, what isn't working as planned is the binding mechanism. As Michael mentioned, the [Bindable] tag is required on the ArrayCollection:

[Bindable]
private var array:ArrayCollection = new ArrayCollection();

Without this, your component will never be notified of any changes, and it will assume that the value of the empty ArrayCollection hasn't changed since it was passed to the setter.


Looking at this, I would say that you have failed to bind your arrayCollection, since you are missing the [Bindable]-tag. Try this:

[Bindable]
private var array:ArrayCollection = new ArrayCollection();

var person1:Person = new Person();
...

That should do the trick.

The reason you are getting 0 as a length for your ArrayCollection is probably because you populate your ArrayCollection after your GridComponent was created and at that given point, your ArrayCollection holds no data yet.


I agree with the above answers which observe that the array collection must be bound in order for the code to be updated when the array collection is populated with values. However, the binding expressions in the above answers work best for scalar values not arrays.

The above binding expressions are triggered only if the array collection itself is replaced with another array collection and not if the contents of the same array collection are updated (as appears to be the case here).

In order to detect changes to the contents of the array, it is necessary to add an event listener for the "collectionChange" event like this array.addEventListener("collectionChange", ...);

(It may be more convenient to use the array as the dataProvider with a DataGroup, because the DataGroup already implements the necessary logic.)


Do you mean that it's telling you that the length is 0 while still inside the setter? (The call to Alert.show() is in there.) You'd be better off putting that in a click handler and then clicking on it to debug it.

Depending on what the rest of your code says, the setter is almost definitely being called after the ArrayCollection is initialized, but before anything is put in there. Essentially:

private var array:ArrayCollection = new ArrayCollection();
.
.
.
<GridComponent object>.array = array; // Alert.show() shows "0".
.
.
.
var person1:Person = new Person();
person1.id = 1;
person1.name = "pavzie1";

var person2:Person = new Person();
person2.id = 2;
person2.name = "pavzie2";

var person3:Person = new Person();
person3.id = 3;
person3.name = "pavzie3";   

array.addItem(person1);
array.addItem(person2);
array.addItem(person3);

So you still have three items in that ArrayCollection, but since you're calling that setter before putting anything in there, you're seeing "0", which is correct. Again this depends on what the rest of your code says.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜