You are correct that you have to use setItemAt to update the ArrayCollection once the result is returned. I tried two ways to accomplish this...an easy way, and a safer way.

1) EASY. You can create a model.selectedProduct OR model.selectedProductID on the model (OR pass a reference through the event and grab it in the Command before the event is dispatched) to hold the item or index of the selected product that you are updating. Obviously, the danger to this is that the selection or collection could change between the call to the server and the response (unless you prevent this).

2) SAFER. The way i decided to do it is to search for the product ID in the ArrayCollection, get back an index and then setItemAt that index.

I created this as a static function and put it in a utilities class.

public static function getItemIndex (ac:ArrayCollection,value:Object,fld:String):int{
                        var arraylen:Number = ac.length;
                        for (var i:int=0;i<arraylen;i++){
                                var test:String = ac.getItemAt(i)[fld];
                                if(ac.getItemAt(i)[fld] == value){
                                        return i;
                                }
                        }
                        return NaN;
                }

Then I do the following each time a get a result back from the server. (Obviously, your object must contain some unique ID)

//next find index of updated record
var index:Number = ArrayCollectionUtil.getItemIndex (model.someList,data.result['someObject'].id,'id');
//set updated record as selected
model.someList.setItemAt(data.result[' someObject'],index);

You could extend your ArrayCollection Class to do this all in one step, but it's not too bad this way. Honestly, this doesn't feel like the cleanest solution, but it does feel safer to do it this way then take the risk that the ArrayCollection or selectedItem could somehow change in the time it takes your result to be returned. Obviously, you would then be updating the wrong item. I guess it all depends on the complexity of your program.

Let me know if you think of a better way to do it.

- Kevin




On Apr 12, 2007, at 6:38 PM, Dan wrote:
Hey thanks,

What I'm trying to reference is the specific object in an array collection that i have stored in the modelLocator. Similar to one of your earlier scenarios I am using a grid to edit a list of products and prices. After a product is edited and the changes are sent and updated on the database side and returning a success to flex i want to update the arraycollection in the modelLocator feeding my grid so that the save is reflected.

It seems to me that you have to use the setItemAt(item:Object, index:int ) function to update the arrayCollection, so I was wondering how you get the index number and what is the best way to pass the new object to this function.


-Dan



On Mar 13, 2007, at 5:11 PM, Kevin wrote:

the solution below works:


On Mar 13, 2007, at 4:48 PM, Kevin wrote:

someone else was saying that I might be able to pass "model.userState.currentUser" as one property "source" and then the field as "fld".

I would then try to call this in the Command: source[fld] = someData;

I haven't tried this yet, but I will let you know if that works.


- kevin



Reply via email to