Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Dynamically adding View components: where should I do it?  (Read 8241 times)
schickm
Newbie
*
Posts: 9


View Profile Email
« on: June 23, 2008, 03:52:59 »

I'm working on a rather large application and so I dont want to actually embed mxml components inside of other mxml components manually.  Instead I would just like to go
:
// TaskPanel is a view component
var taskPanel:TaskPanel = new TaskPanel();
taskPanel.name = TaskPanel.NAME;
// app is a reference to the main Application
app.addChild(taskPanel);
facade.registerMediator( new TaskPanelMediator(app.getChildByName(TaskPanel.NAME) ));

So I'm thinking that I should do this stuff inside of a controller.  Like say for instance I want to have a menu added dynamically I would create an controller called "ShowMenuCommand" and then do a sendNotification whenever I want it up.  The problem is that I'm having trouble getting a reference to the main Application.  I tried this:
:
var appMed:ApplicationMediator = facade.retrieveMediator(ApplicationMediator.NAME) as ApplicationMediator;
var myApp:MyApplication = appMed.app();
myApp.addChild(menu);

but I get this error on the second line above:
:
Attempted access of inaccessible method app through a reference with static type com.onshored.planq.view:ApplicationMediator.

Any ideas?  Am I approaching this completely wrong?

 
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: June 23, 2008, 06:59:14 »

You're making a method call on the ApplicationMediator. app is probably defined as an implicit getter and therefore would be accessed like a property. Also, you should not retrieve the mediator and set this from a Command it breaks the encapsulation of the mediator. Instead, create the child object and send it as the body of a notification that the ApplicationMediator is interested in and let it call app.addChild.

-=Cliff>
Logged
schickm
Newbie
*
Posts: 9


View Profile Email
« Reply #2 on: June 24, 2008, 08:28:51 »

Hah, that makes complete sense.  I felt like I was doing something wrong when I was trying to do it from the controller.  I now have it setup where my ViewPrepCommand sends a notification that the ApplicationMediator picks up on.

thanks for the quick and easy to understand reply Cliff!  Your framework is the best for as3.
Logged
Pages: [1]
Print