0
votes

I use a Label component to display the length of an ArrayCollection. How do I get it to update when I add new items to the collection?

Here's the text field for the Label:

text="{model.meetingInfo.documentList.length}"

Here's the handler for adding a new item to the collection:

var attachmentProgressVO:AttachmentProgressVO = new AttachmentProgressVO();
                attachmentProgressVO.fileReference = file as File;
                newAttachmentList.addItem(attachmentProgressVO);
                checkIfUpdate(file as File, attachmentProgressVO);
                meetingInfo.docsAndAttachmentsList.addItem(attachmentProgressVO);

I tried adding these 2 lines but that didn't work:

meetingInfo.docsAndAttachmentsList.itemUpdated( attachmentProgressVO );
                meetingInfo.docsAndAttachmentsList.refresh();

I also tried changing this:

public var docsAndAttachmentsList:ArrayCollection = new ArrayCollection();

to this:

private var _docsAndAttachmentsList:ArrayCollection = new ArrayCollection();

..with a getter and setter but that didn't work.

I'm not using the right approach, am I?

1
I do not believe that Array.length is a bindable property (which is good, since the length getter is very resource intensive, unfortunately). If this is true, it won't automatically update. You'll need to manually update the field, I think. - Josh
@Apocalyptic0n3 Length, on an ArrayCollection, is Bindable. Despite being a read only property. it is set up to trigger binding in association with the CollectionChange event. length on an array would not be Bindable since Bindable is a Flex Framework specific implementation and Array is not a Flex class. Why do you think the length getter is resource intensive? I never tested it, but never noticed problems either. ArrayCollection.length is just a wrapper of Array.length; so it should be really quick--unless Array.length is what causes the slowdown. - JeffryHouser
I have not done any tests either, but I have read in multiple locations that it is a "heavy" getter and that it is best not to use it as a limiter in a loop because it has to be run each and every time the loop runs. I'm unsure how they get the length, but I suspect it may use a loop to count each item in the array. - Josh

1 Answers

1
votes

Generically, Binding only looks at a specific object; you can't drill down 4 objects deep to a specific property and expect binding to update values.

Changing the documentList does not change meetingInfo or Model, so binding will never be triggered. itemUpdated() and refresh() should update the list based class which displays the data; but will not affect your label displaying the count.

You need to listen on the collection for a collectionChange event and manually update the label's text in the collectionChange handler.