Prana Array and Method-Invocation
Architecture November 2nd, 2008In the next example I extended the previous example with declaring an array and invoking a method on an instance of an object.
In the ModelLocator I added two variables which will be manipulated by the prana container.
1 2 | public var users:Array; public var typedUsers:TypedCollection; |
Users is an Array, typedUsers is a TypedCollection of User objects (see the Constructor).
There’s also a function to add an instance of a User to the typedUsers collection.
1 2 3 4 | public function addUser(user:User):void
{
this.typedUsers.addItem(user);
} |
In the PranaExample.mxml (application starting point) I added two List controls with dataproviders users and typedUsers.
1 2 3 4 5 6 7 8 | <mx:List dataProvider="{JohleroModelLocator.getInstance().users}" labelField="name" rowCount="{JohleroModelLocator.getInstance().users.length}" /> <mx:List dataProvider="{JohleroModelLocator.getInstance().typedUsers}" labelField="name" rowCount="{JohleroModelLocator.getInstance().typedUsers.length}" /> |
The two variables (users and typedUsers) are being populated by the Prana Framework like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <object id="johleroModelLocator" class="johlero.model.JohleroModelLocator" factory-method="getInstance"> ... <!-- Example declaring an array --> <property name="users"> <array> <value> <object class="johlero.model.User"> <constructor-arg value="Johlero"/> </object> </value> <value> <object class="johlero.model.User"> <constructor-arg value="John"/> </object> </value> <value> <object class="johlero.model.User"> <constructor-arg value="Mark"/> </object> </value> </array> </property> <!-- Example using method-invocation --> <method-invocation name="addUser"> <arg> <object class="johlero.model.User"> <constructor-arg value="Johlero"/> </object> </arg> </method-invocation> </object> |
As you can see the users array is just populated by an array tag with multiple value tags.
To populate the typedUsers TypedCollection, we need to call the addUser function on the JohleroModelLocater. The arg tag defines the argument that will be passed to the addUser function.
In the lib there’s a prana-main.swc. This swc is not the same as the swc of the latest production release from prana. There was a bug and it’s fixed in this swc. You can allways checkout the prana repository from https://prana.svn.sourceforge.net/svnroot/prana/prana-main/trunk and add a reference to your project. Like that you’ll allways have the latest code available (with its risks).
That’s it. See the example and project to see it work!
Ciao! Lieven Cardoen aka Johlero
Follow Me!