Question

In flex, I'm using a component which displays a specific item (selectedItem field) from its dataProvider (ListCollectionView).

The elements of my dataProvider can be edited by different means. Each time one is edited, an update event is sent, leading to update the dataProvider collection (ListCollectionView::listChangeHandler).

In the process, if my selectedItem is the edited element, it is erased (set to null via the ListCollectionView::moveItemInView), even if the edited field is used nowhere.

Is there a way to avoid this ?

More specifically without having to save it before, having it lost and restoring it afterwards (which would be hardly practicable here)


Edit : Okay, I think I can explain further :

In my code, I make an affectation on a property of one of the items of the list which is databinded to the dataProvider. I don't know exactly why, probably because the class of the item is [bindable], this throws an update event.

This event is caught by the ArrayList::itemUpdateHandler which dispatches it to the ListCollectionView::listChangeHandler. At this point, the event "kind" is "update", which seems ok to me…

But from there, it goes to ListCollectionView::moveItemInView (I suppose that it makes sens as the item should have to update its position if the list is sorted). This function removes the item from it's former location and added it to its new location.

As it removes it, it sends a new Event with "remove" kind. And this is where the thing goes wrong : my component listens the remove events of the data it uses and removes its selectedItem if this element is removed.

I think it is around the call to moveItemInView that there is a problem : the call in ListCollectionView::handlePropertyChangeEvents (called by listChangeHandler) is : moveItemInView(updateEntry.item, updateEntry.item, eventItems); (where updateEntry.item is the modified item of the list and eventItems an empty array)

Whereas its prototype is : private function moveItemInView(item:Object, dispatch:Boolean = true, updateEventItems:Array = null):void

Is that a bug ?

Était-ce utile?

La solution

How do you update the dataProvider? If it is this

list.dataProvider = newDataProvider

Then the behavior you see is expected. From the List perspective, you have completely replaced the dataProvider and the List has no way of knowing if the old selectedItem/selectedIndex will still be valid. I think it makes the correct assumption in assuming that it is not.

Or do you update the dataProvider using the dataProvider's refresh method:

list.dataProvider.refresh();

In such a case the dataProvider's length could change; as could its elements. In this case a collectionChange event will be dispatched and the list should have a method to handle the collectionChange event. The List has no way to know whether the previous selected item will be in the new list; whether its index will change, or even if the old selectedIndex will still be valid. So, the selectedItem gets reset.

Generally to preserve the selectedItem/selectedIndex in either case, you should store a reference to it and reset it after the dataProvider change:

mySelectedItem = list.dataProvider.selectedItem;
// update dataProvider Here
list.dataProvider.selectedItem = mySelectedItem;

If you're just updating an item in the dataProvider; then the selectedIndex/selectedItem should not be affected.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top