Wow, I've got to say; "What a mess." However, you can get it to work
with the following changes:
[Bindable]
private var customMeals:XMLListCollection = new XMLListCollection();
// addToMeal event Handler
private function addToMealHandler(event:Event):void
{
customMeals.addItem((event.target as
Panelcreatemealitem).mealDetails);
Alert.show(customMeals.toString());
}
You have some naming conflicts and, since you're dealing with xml,
you'll need to use an XMLListCollection; instead of an ArrayCollection.
-TH
--- In [email protected], "Jason B" <nos...@...> wrote:
>
> Heres my three mxml files two of which are under the com/ folder and
get imported...hope that helps
>
>
> <?xml version="1.0" encoding="utf-8"?>
> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
> creationComplete="init()" styleName="plain"
xmlns:nutrition="com.nutrition.*"
> paddingLeft="10" paddingTop="10" paddingRight="10" paddingBottom="10">
>
> <mx:Script>
> <![CDATA[
> import mx.collections.ArrayCollection;
> import mx.collections.ICollectionView;
> import mx.collections.XMLListCollection;
> import com.nutrition.MealItem;
> import mx.controls.Alert;
> import mx.rpc.events.FaultEvent;
> import mx.rpc.events.ResultEvent;
> import mx.rpc.http.HTTPService;
> // initialize
> private function init():void
> {
> //load calories
> httpservice_load("/main.php/flexnutrition/loadcalories",
this["loadcaloriesResultHandler"]);
>
> hsNutrition.send();
> addEventListener('addToMeal', addToMealHandler);
> addEventListener('removeMeal', removeMealHandler);
> addEventListener('learnAboutThis', learnaboutthisswf);
>
>
>
> }
>
> private function httpservice_load(url_src:String,
resulthandler:Function):void{
>
> httpService.url = url_src;
>
> //var tmp:String = resulthandler.toString();
> //if(tmp != "none")
> httpService.addEventListener(ResultEvent.RESULT, resulthandler);
> httpService.send();
> }
>
> private function learnaboutthisswf(event:Event):void{
> vfc_guy.load("/uploads/swf/" + loadswf_var);
>
> }
>
> [Bindable]
> private var nutritionData:XML;
> [Bindable]
> private var maindata:XML;
> [Bindable]
> public var loadswf_var:String;
> [Bindable]
> public var createmeals_loadswf_var:String;
>
> //load all calories for main meals
> private function loadcaloriesResultHandler(event:ResultEvent):void
> {
> maindata = event.result as XML;
> vfc_guy.load("/uploads/swf/" + maindata.swf);
> }
>
> // nutrition result handler
> private function nutritionResultHandler(event:ResultEvent):void
> {
> nutritionData = event.result as XML;
> }
> // nutrition fault handler
> private function nutritionFaultHandler(event:FaultEvent):void
> {
> Alert.show(event.toString());
> }
>
> // create breakfast
> private function createBreakfast():void
> {
> vsMain.selectedIndex = 1;
> plantype = 'Breakfast';
> }
> // create lunch
> private function createLunch():void
> {
> vsMain.selectedIndex = 1;
> plantype = 'Lunch';
> }
> // create dinner
> private function createDinner():void
> {
> vsMain.selectedIndex = 1;
> plantype = 'Dinner';
> }
>
> [Bindable]
> private var plantype:String;
>
> [Bindable]
> private var customMeals:ArrayCollection = new ArrayCollection;
>
> // addToMeal event Handler
> private function addToMealHandler(event:Event):void
> {
> trace(event.target);
> //customMeals.addItem((event.target as mealitem).mealDetails);
> Alert.show(event.toString());
>
> }
>
> // removeMeal event Handler
> private function removeMealHandler(event:Event):void
> {
> customMeals.removeItemAt(ls.selectedIndex);
> }
>
> ]]>
> </mx:Script>
>
> <mx:HTTPService id="hsNutrition"
url="/main.php/flexnutrition/nutritioncal"
> result="nutritionResultHandler(event)"
> fault="nutritionFaultHandler(event)" resultFormat="e4x"/>
>
> <mx:HTTPService id="httpService" url="" resultFormat="e4x"/>
>
> <mx:Label id="totalcalories" text="{'Today\'s Meal Plan is based on a
' + maindata.totalcalories + ' Calorie Diet'}" fontWeight="bold"
fontSize="18"/>
>
> <!--mx:Button label="Print Meal Menu" /-->
>
> <mx:ViewStack id="vsMain" width="100%" height="100%">
>
> <mx:Canvas id="nutrition_canvas" width="100%" height="100%">
>
> <mx:SWFLoader id="vfc_guy" source="" height="400" width="400"
autoLoad="true" x="700" y="100" />
>
> <mx:HBox width="100%" height="100%">
>
> <mx:VBox width="40%" height="100%">
>
> <nutrition:MealPanel id="pnlBreakfast"
meal="{nutritionData.breakfast}"
> title="Breakfast : Please select one meal from
below({maindata.breakfast} Cal Goal)"/>
>
> <mx:HBox horizontalCenter="right">
> <mx:Button label="Create your own Breakfast"
click="createBreakfast()"/>
> </mx:HBox>
>
>
> <nutrition:MealPanel id="pnlSnack1" meal="{nutritionData.snack1}"
> title="Snack : Please select one snack from below({maindata.snack1}
Cal Goal)"/>
> <!--mx:Button label="Create your own Lunch" click="createLunch()"/-->
>
> <nutrition:MealPanel id="pnlLunch" meal="{nutritionData.lunch}"
> title="Lunch : Please select one meal from below({maindata.lunch} Cal
Goal)"/>
>
> <nutrition:MealPanel id="pnlSnack2" meal="{nutritionData.snack2}"
> title="Snack : Please select one snack from below({maindata.snack2}
Cal Goal)"/>
> <!--mx:Button label="Create your own Dinner"
click="createDinner()"/-->
>
> <nutrition:MealPanel id="pnlDinner" meal="{nutritionData.dinner}"
> title="Dinner : Please select one meal from below({maindata.dinner}
Cal Goal)"/>
>
> <nutrition:MealPanel id="pnlSnack3" meal="{nutritionData.snack3}"
> title="Snack : Please select one snack from below({maindata.snack3}
Cal Goal)"/>
> </mx:VBox>
>
>
> </mx:HBox>
> </mx:Canvas>
>
>
> <mx:HBox width="100%" height="100%">
>
> <mx:VBox width="30%" height="100%">
> <mx:HBox>
> <mx:Label text="{'My Custom ' + plantype}"/>
>
> </mx:HBox>
>
> <mx:List id="ls" dataProvider="{customMeals}" width="100%">
> <mx:itemRenderer>
> <mx:Component>
> <mx:VBox>
> <mx:Image width="100" height="100"/>
> <mx:Button label="Remove" click="dispatchEvent(new Event('removeMeal',
true))"/>
> <mx:Label text="{data.serving}"/>
> </mx:VBox>
> </mx:Component>
> </mx:itemRenderer>
> </mx:List>
> </mx:VBox>
>
> <mx:VBox width="100%" height="100%">
>
> <!--mx:Label text="Please select ONE ITEM from list below"/-->
> <mx:ViewStack width="40%" height="410" borderStyle="inset"
borderThickness="2" id="vsCreateMeals">
>
> <mx:VBox >
> <nutrition:Panelcreatemeal id="pnlCreateBreakfast"
meal="{nutritionData.breakfast}"/>
> </mx:VBox>
>
> <mx:VBox >
> <nutrition:Panelcreatemeal id="pnlCreateLunch"
meal="{nutritionData.lunch}"/>
> </mx:VBox>
>
> <mx:VBox >
> <nutrition:Panelcreatemeal id="pnlCreateDinner"
meal="{nutritionData.dinner}"/>
> </mx:VBox>
>
> </mx:ViewStack>
> <mx:HBox>
> <mx:Button label="Save" click="vsMain.selectedIndex = 0;"/>
> <mx:Button label="Cancel" click="vsMain.selectedIndex = 0;"/>
> </mx:HBox>
>
> </mx:VBox>
>
>
> </mx:HBox>
>
> </mx:ViewStack>
>
> </mx:Application>
>
>
>
>
>
> <?xml version="1.0" encoding="utf-8"?>
> <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
height="390"
> borderThicknessLeft="0" borderThicknessRight="0"
borderThicknessBottom="0"
> dropShadowEnabled="false"
> horizontalAlign="center"
xmlns:containers="com.dougmccune.containers.*" title="Select An Item
from below">
>
> <mx:Script>
> <![CDATA[
> //import com.createmealPanel;
>
> [Bindable]
> private var _meal:XMLList;
>
> public function set meal(value:XMLList):void
> {
> if(value)
> {
> _meal = value;
> createContainers();
> }
> }
>
> private function createContainers():void
> {
> for each(var item:Object in _meal.itemdata)
> {
> if(item.available == "yes")
> {
> var mealitem:Panelcreatemealitem = new Panelcreatemealitem;
> coverflow.addChild(mealitem);
> mealitem.mealDetails = item;
> }
> }
> }
> ]]>
> </mx:Script>
>
> <containers:CoverFlowContainer id="coverflow" width="100%"
height="100%"
> horizontalGap="1" borderStyle="inset" backgroundColor="0xFFFFFF"
> segments="9" reflectionEnabled="true"/>
>
> <mx:HBox>
> <mx:Button label="item 1"
click="parentApplication.vsCreateMeals.selectedIndex=0;"/>
> <mx:Button label="side item 1"
click="parentApplication.vsCreateMeals.selectedIndex=1;"/>
> <mx:Button label="side item 2"
click="parentApplication.vsCreateMeals.selectedIndex=2;"/>
> </mx:HBox>
> </mx:Panel>
>
>
>
>
> <?xml version="1.0" encoding="utf-8"?>
> <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" width="310"
height="320"
> title="{mealDetails.title}" horizontalAlign="center">
>
>
> <mx:Metadata>
> [Event(name="addToMeal")]
> [Event(name="learnAboutThis")]
> </mx:Metadata>
>
> <mx:Script>
> <![CDATA[
> import mx.controls.Alert;
>
> [Bindable]
> public var mealDetails:Object;
>
>
> private function calldispatch():void {
> parentApplication.createmeals_loadswf_var = mealDetails.swf;
> dispatchEvent(new Event('learnAboutThis', true));
>
> }
>
> ]]>
> </mx:Script>
>
> <mx:Image source="{'/uploads/nutrition/' + mealDetails.imgsource }"
width="285" height="159"/>
>
> <mx:Label text="{mealDetails.serving}"/>
>
> <mx:Button label="Add to Meal" click="dispatchEvent(new
Event('addToMeal', true))"/>
>
> <mx:Button label="Learn About This" click="calldispatch()"/>
>
> </mx:Panel>
>
>
>
> --- In [email protected], Sam Lai samuel.lai@ wrote:
> >
> > It seems like wherever this code is -
> >
> > [Bindable]
> > private var customMeals:ArrayCollection = new ArrayCollection;
> >
> > //throws error on this line
> > customMeals.addItem((event.target as mealitem).mealDetails);
> >
> > event.target is actually the panel containing the 'create meal item'
> > button, as opposed to the mealitem itself. Can you post some more of
> > the script and MXML for that part?
> >
> > The src folder is empty for me too in the ZIP file.
> >
> > 2009/7/2 Jason B nospam@:
> > > yes heres the results
> > >
"Main0.vsMain.HBox8.VBox968.vsCreateMeals.VBox971.pnlCreateBreakfast.cov\
erflow.Panelcreatemealitem1006"
> > >
> > > How can I access the object data, it's in a file thats under the
folder /com/nutrition/file.mxml
> > >
> > >
> > >
> > > --- In [email protected], Sam Lai <samuel.lai@> wrote:
> > >>
> > >> I'd add a trace statement just before it to see what type of
object
> > >> event.target is - it could be that the target is the UI
component, and
> > >> not the data object.
> > >>
> > >> //throws error on this line
> > >> trace(event.target);
> > >> customMeals.addItem((event.target as mealitem).mealDetails);
> > >>
> > >> 2009/7/2 Jason B <nospam@>:
> > >> > I've got an problem when the user clicks on button "Add to
Meal" it throws an error because it can't seem to reference the data in
mealDetails object inside the com/ directory?
> > >> >
> > >> > I'm trying to add the item to the list control.
> > >> >
> > >> > I have the code located here in case you want more details
> > >> > http://files.getdropbox.com/u/228472/mealsflex.zip
> > >> >
> > >> >
> > >> > [Bindable]
> > >> > private var customMeals:ArrayCollection = new ArrayCollection;
> > >> >
> > >> > //throws error on this line
> > >> > customMeals.addItem((event.target as mealitem).mealDetails);
> > >> >
> > >> >
> > >> >
> > >> >
> > >> > <?xml version="1.0" encoding="utf-8"?>
> > >> > <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" width="310"
height="320"
> > >> > title="{mealDetails.title}" horizontalAlign="center">
> > >> >
> > >> >
> > >> > <mx:Metadata>
> > >> > [Event(name="addToMeal")]
> > >> > [Event(name="learnAboutThis")]
> > >> > </mx:Metadata>
> > >> >
> > >> > <mx:Script>
> > >> > <![CDATA[
> > >> > import mx.controls.Alert;
> > >> >
> > >> > [Bindable]
> > >> > public var mealDetails:Object;
> > >> >
> > >> >
> > >> > private function calldispatch():void {
> > >> >
parentApplication.createmeals_loadswf_var = mealDetails.swf;
> > >> > dispatchEvent(new
Event('learnAboutThis', true));
> > >> >
> > >> > }
> > >> >
> > >> > ]]>
> > >> > </mx:Script>
> > >> >
> > >> > <mx:Image source="{'/uploads/nutrition/' +
mealDetails.imgsource }" width="285" height="159"/>
> > >> >
> > >> > <mx:Label text="{mealDetails.serving}"/>
> > >> >
> > >> > <mx:Button label="Add to Meal" click="dispatchEvent(new
Event('addToMeal', true))"/>
> > >> >
> > >> > <mx:Button label="Learn About This"
click="calldispatch()"/>
> > >> >
> > >> > </mx:Panel>
> > >> >
> > >> >
> > >> >
> > >> >
> > >> > ------------------------------------
> > >> >
> > >> > --
> > >> > Flexcoders Mailing List
> > >> > FAQ:
http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
> > >> > Alternative FAQ location:
https://share.acrobat.com/adc/document.do?docid=942dbdc8-e469-446f-b4cf-\
1e62079f6847
> > >> > Search Archives:
http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups
Links
> > >> >
> > >> >
> > >> >
> > >> >
> > >>
> > >
> > >
> > >
> > >
> > > ------------------------------------
> > >
> > > --
> > > Flexcoders Mailing List
> > > FAQ:
http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
> > > Alternative FAQ location:
https://share.acrobat.com/adc/document.do?docid=942dbdc8-e469-446f-b4cf-\
1e62079f6847
> > > Search Archives:
http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups
Links
> > >
> > >
> > >
> > >
> >
>