A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Is it possible to bind a hash of objects to a list?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    10

    Is it possible to bind a hash of objects to a list?

    I have a ArrayCollection of objects..

    Code:
    	<fx:Declarations>
    		<mx:ArrayCollection id="myUserArray">
    			<fx:Object id="1" name="Alfred" lastName="Smith" />
    			<fx:Object id="2" name="Barney" lastName="Jones" />
    			<fx:Object id="3" name="Cesar" lastName="Davis" />
    			<fx:Object id="4" name="David" lastName="Cooper"/>
    		</mx:ArrayCollection>
    	</fx:Declarations>
    ..that is bound to a list:

    Code:
    <s:List id="list1" height="230" width="150" itemRenderer="itemrenderer" dataProvider="{myObject}">
    Suppose I want to update object with id x. Do I have to loop trought all the objects to determine which object to
    update? The object is not selected so I dont know the index of it. (Notification to update it comes by network).

    I would prefer to have a hash/associative array bound to objects in so that I could directly access a specific object in an array by its id. Then it would be so nice and beautyfull to directly have the possibilty to update values without looping with syntax like myUserArray[id].name="new name" and perhaps remove objects by myUserArray.remove(id).

    Is something like this possible? Do you have any suggestions on this topic?
    Last edited by olarsson; 02-15-2014 at 08:16 PM.

  2. #2
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Hmm.. those "id" where ment to be properties of the object but objects offcourse have the reserved word "id" for their object reference which makes that they can be accessed directly by the object reference.

    The tricky part would then be to transform a string to an object reference.

    If I create an obejct:

    <fx:Object id="p1" id_number="1" name="Alfred" lastName="Smith" />

    and have it in an array or similar, I can still access it by

    p1.name="new name"

    ..etc.

    Now the tricky part for me would be to change a string value which I get from a function "p1" to an object reference which I could use programmaticly.
    Last edited by olarsson; 02-15-2014 at 09:19 PM.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    I now have found out and tested that it seems to be possible to access any object of a flex program by using the object id like this..

    var wanted_object:Object=this["id"]

    ..crazy.

  4. #4
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    I might not get what you're asking but....

    You want to parse the xml into an array of objects - lots of examples out there. The xml gets loaded into an object but referencing things isn't pretty so turn the xml data into the data you need when the xml loads. So referencing your xml data could look like this:

    myXML[i].id
    myXML[i].name
    myXML[i].lastname

    You can reference things dynamically.


    myObject.myFirstName = "Fred";
    myString:String = "Name";

    myObejct.myFirstName can be referenced like this: myObject["my"+"First" + myString]

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Quote Originally Posted by moot View Post
    I might not get what you're asking but....

    You want to parse the xml into an array of objects - lots of examples out there. The xml gets loaded into an object but referencing things isn't pretty so turn the xml data into the data you need when the xml loads. So referencing your xml data could look like this:

    myXML[i].id
    myXML[i].name
    myXML[i].lastname

    You can reference things dynamically.


    myObject.myFirstName = "Fred";
    myString:String = "Name";

    myObejct.myFirstName can be referenced like this: myObject["my"+"First" + myString]
    Thank you for your answer. Sorry for not explaining my problem clear enough.

    My problem is that I have an array bound to a list. If I update the array I can refresh the list and changes will be visible.

    If I got a idientifier like "id_number" for an object in the array I would like to be able to update the correct object easy without having to loop through all the objects in the array to see which of them matches the identifier. This is possible to do if you have a dictionary and a list.

    When creating object and adding them to the array you also put the object identifier as a key in the dictionary and the object reference as the value; dict[identifier]=newobject;

    Then when you want to update an item with only identifier string aviable you only write

    dict[identifier].paramtoupdate=newvalue

    The only problem then is that you have two datastructures dictionary and array that need to be updated.

  6. #6
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    If the xml is so large that you don't want to loop through it, the xml needs to be changed.

    When you parse the xml, build arrays of the data you need to change and use those arrays instead of the main array.

    There's a prebuilt array sort in actionscript - sort(). You can sort the array on whatever property(s) of the array you need and get to the correct one quicker.

    aDeckOfCards.sortOn(["sCardSuit", "sCardNumber"], [Array.DESCENDING, Array.DESCENDING | Array.NUMERIC]);

    sort and sortOn are both string sorts by default. You have to say when it's a numeric search.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center