Wednesday, December 1, 2010

Battle For MVC in Flex 4 : Pass by reference example

It is not impossible to find a reference to 'passing by reference' in the online documentation, but it is not easy. I scoured unrelated code and rented books to find the syntax for the most useful passing by reference syntax. (sorry, meta terms about language makes for awful reading) In Flex 4, this syntax is a necessary component to building a Flex with a MVC pattern. So here's what I've found, just for posterity's sake.


Simple Object Reference

< controllers:IndexController id="controller" playButton="{playButton}" />
< grumbles:PlayButton x="79" y="194" id="playButton" />


The {} syntax lets reference any object declared in your current mxml file, but it requires you name all your objects with the id parameter. Id is the parameter that flex uses to tie everything together. You can also reference member variables of names objects, much the same way you do in actionscript.


< controllers:IndexController id="controller" fooButtonId="{fooButton.id}" />
< grumbles:fooButton x="79" y="194" id="myCrazyId" />



Array of Object Reference

< controllers:MusicExerciseController id="controller" notesArray="{[e1,f1,g1]}"/>
< grumbles:note id="e1"/>
< grumbles:note id="f1"/>
< grumbles:note id="g1"/>


In this example we want a reference to a set of visual items, in this case a set
of musical notes. The member variable 'notesArray' is defined as an Array object. The {[]} notation is the key take away here.


Embedding Resources in Mxml

< grumbles:note id="a1Sharp" pictureSource="@Embed('../includes/notePicture.png')" />


This is similar to embedding resources in actionscript but slightly simplified. If you're using Flash Builder 4, it will kindly let you know if you resource doesn't exist at the location you specified.

I'm sure there's more reference syntax out there, but this is the core of what I've needed to make MVC work.

No comments:

Post a Comment