1

[This will take you to an image of my project so that you may see the code because I haven't been a member long enough to post one. :]1

This is a project I'm working on for a simple demo tutorial application in Flex. I am using mxml pages. You can see to the left that I will have my main application and then other mxml files (In the Examples Package) as the examples that will be loaded into the main application mxml. How can I dynamically pull the value from the object in the list to load the child mxml file into the container that I have further down in the application?

GuateCoder
  • 13
  • 4
  • That's impossible. ActionScript - and hence Flex - is a compiled language. You can't just load an mxml file and have it run; it must be compiled first. Which means that if you compile it into a swf, you _can_ load that swf file and add that to your displaylist. – RIAstar Mar 09 '12 at 22:08
  • Well, actually it could be done with something like the [as3-commons-bytecode](http://www.as3commons.org/as3-commons-bytecode/index.html) library, but that would definitly not be a simple demo application. – RIAstar Mar 09 '12 at 22:12
  • @RIAstar I think--but am not sure--that you may be overcomplicating it. If he has a bunch of MXML components, why not just display them in a ViewStack and change the ViewStack depending on the sample you want to show? – JeffryHouser Mar 09 '12 at 22:36
  • @www.Flextras.com Indeed I missed the underlying question and only focussed on the exact question as described in the OP's code sample. – RIAstar Mar 10 '12 at 09:33

1 Answers1

2

I'm unclear exactly what you're trying to do; but based on the code snippet I see two possibilities.

First, if all of your separate samples are compiled into individual SWFs; then you can load them using the SWFLoader. Based on the XML in your code snippet, it seems to reference separate SWFs.

If you're code samples are just compiled into the main application, then you can use a ViewStack and switch the index of the ViewStack depending on the sample you want to show. Conceptually something like this:

<mx:ViewStack id="sampleViewStack">
  <myComps:Sample1 />
  <myComps:Sample2 />
  <myComps:Sample3 />
</mx:ViewStack>

To show sample one, just do this in ActionScript:

sampleViewStack.selectedIndex = 0;

To show sample 2, do this:

sampleViewStack.selectedIndex = 2;

And so on...

However, if you want to compile each sample on the fly and display it in your main application you'll have a much harder job.


If I understand correctly, You said you're having a hard time accessing properties on the object that are in the lists dataProvider. Based on your screenshot of the code, it looks like your dataProvider is made up of generic objects. To access the label or value properties on those objects; you'll; have to do this:

list.selectedItem['label']
JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
  • Ok that is making something show up but now I'm not sure how to access the value of my object in the list. Here is the code snippet. https://plus.google.com/u/0/photos/113002783589802349422/albums/5718014021726928433/5718093762131711906 – GuateCoder Mar 10 '12 at 02:45
  • You can access the selectedItem in a list by using list.selectedItem . What value do you want to access? – JeffryHouser Mar 10 '12 at 13:02
  • Yes I can access the selected item but how can I access the value of the selected item? The .valueOf() method doesn't work and there is no accessor for the value of the object in the list. – GuateCoder Mar 10 '12 at 23:38
  • I'm not sure I understand, and I have no idea how to rephrase my question. list.selectedItem will give you the item that is currently selected in the list. What is the dataProvider for your list? And what value are you trying to access? – JeffryHouser Mar 10 '12 at 23:42
  • Inside of my list I have objects. Each one of those objects have a string value for the attribute value. I'm trying to access that string value of the selected item but it won't let me. – GuateCoder Mar 11 '12 at 02:08
  • I'm guessing at this point, but edited my answer to show you how to access properties on generic objects. You say "inside your list you have objects" But I'm not sure what that means. That is true, but it doesn't tell me what you want to access. Do you mean your list's dataProvider is made up of objects? Or are you referring to the itemRenderers that display the data? Or are you referring to the scrollbar? All of those things could be considered 'objects in a List'. – JeffryHouser Mar 11 '12 at 02:55
  • I am referring to the data Provider is made up of objects. If you refer to the most recent code snippet, within the list there are objects and each object has a label and a value. I am trying to access the value so that I may pass it in and dynamically change the view of my application. – GuateCoder Mar 11 '12 at 03:21
  • I showed you how to access that value in my edited answer. Really, you're code should be part of the question not linked to a screenshot on some external service. – JeffryHouser Mar 11 '12 at 04:27
  • If I was able to post it here then I would but I haven't been a member of StackOverflow long enough because when i do try to upload an image it won't let me. – GuateCoder Mar 11 '12 at 18:28
  • Don't upload an image of the code; add the actual code text to your question. – JeffryHouser Mar 11 '12 at 19:49
  • I have the following swfLoader in my mxml: Then I have the following code as the event that fires when the index changes on the list. This is the event function in my script tag public function swfLoader(event:Event):void { var item:String = list.selectedItem['value']; trace(item); Load.source = "@Embed(source='" + item + "')"; //Load.source = "@Embed(source='example.swf')"; } – GuateCoder Mar 11 '12 at 21:31
  • Sorry i guess I should include the question. When I try to pass in the source of the selected item into the swfLoader's source it is not loading correctly. Does it seem to be correct or am I going about it wrong? – GuateCoder Mar 11 '12 at 21:57