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: Casting the View Component Implicitly  (Read 8371 times)
kofiaddaquay
Full Member
***
Posts: 31


View Profile Email
« on: September 17, 2008, 10:15:45 »

Hi...:) I have a question about the above subject. i am a little confused and need a little clarification

best practices documentation mentions this

"Your concrete Mediator’s constructor will pass its View Component to the superclass and it will be made immediately available internally as a protected property called viewComponent, generically typed as Object."

so for instance, IF i have an mxml component called LoginPanel and its mediator LoginPanelMediator, the constructor for the mediator should like this

...
public function LoginPanelMediator ( viewComponent : Object ) : void
{
     super(NAME, viewComponent);
     ...
}

then the documentation goes on to say

"However it was set, you will frequently need to cast this Object to its actual type, in order to access whatever API it exposes, which can be a cumbersome and repetitive practice."

so like this

protected function get loginPanel() : LoginPanel
{
     return viewComponent as LoginPanel;
}


this makes sense...until i start seeing mediator constructors in some of the sample applications like this

public function LoginPanelMediator ( viewComponent : LoginPanel ) : void
{
     super(NAME, viewComponent);
     ...
}

then casted this same old way

protected function get loginPanel() : LoginPanel
{
     return viewComponent as LoginPanel;
}

notice the type for the viewComponent in the contructor. this time its not type Object BUT LoginPanel. does it really matter.?? are both implementations right? :-\ :-\

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



View Profile WWW Email
« Reply #1 on: September 18, 2008, 08:14:54 »

The constructor for the Mediator framework class takes an Object for the viewComponent. It takes this and sets it on the inherited 'viewComponent' property.

Your subclass may accept an object of any type as a constructor argument. Since all objects in actionscript ultimately extend Object, all object types can be passed to the superclass constructor, and will be downcast to Object in the process.

Since the inherited 'viewComponent' property is type Object, you need the typed getter when you reference it.
 
As for the subclass constructor, if you specify the viewComponent parameter type, then you ensure your mediator cannot be constructed with just any component.

-=Cliff>
Logged
kofiaddaquay
Full Member
***
Posts: 31


View Profile Email
« Reply #2 on: September 18, 2008, 10:44:40 »

Hmm, makes sense. Thanks Cliff...one other question based on what you said in your post

"As for the subclass constructor, if you specify the viewComponent parameter type, then you ensure your mediator cannot be constructed with just any component."

wouldnt you in any case want ensure that your mediator cannot be constructed with just any component? I mean, if your LoginPanel mxml Component whose Mediator is LoginPanelMediator ...setting the constructor as

public function LoginPanelMediator ( viewComponent : LoginPanel ) : void
{
     super(NAME, viewComponent);
     ...
}

would ensure your statement above and not setting the viewComponent type as object. right? would i be better of always doing this?

« Last Edit: September 18, 2008, 11:14:44 by kofiaddaquay » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #3 on: September 18, 2008, 02:27:04 »

Yes, you'd generally want to do this.

-=Cliff>
Logged
kofiaddaquay
Full Member
***
Posts: 31


View Profile Email
« Reply #4 on: September 18, 2008, 08:04:22 »

as always... thanks for your time

Kofi
Logged
Pages: [1]
Print