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: renaming mediators - why not?  (Read 4598 times)
sirGordon
Newbie
*
Posts: 4


View Profile Email
« on: February 16, 2011, 12:04:22 »

lets say i have an application where users can edit documents
every document gets a unique id once it has been saved
the document cannot be opened if already open by the user

i have a facade for document edit module
when a user opens a document, a document edit mediator is created and registered with mediatorName = document id
the problem begins:
1. user clicks "create new document", which does not have an id yet
2. a new document edit window is opened with lets say some UID as temporary mediatorName
3. user saves the document and does not close it
4. user tries to open the same document and succeeds, becuause i check facade.hasMediator(<id of the document>) which returns false

i dont want to double-check / keep my own window map / stuff like that, i'd rather just rename the mediator.
for this purpose ive extended puremvc a bit
here is all i wrote:
:
public class ExtendedFacade extends Facade {
public function ExtendedFacade(multitonKey : String) {
super(multitonKey);
view = ExtendedView.getInstance(multitonKey);
}

override protected function initializeView():void {
if (view != null) return;
view = ExtendedView.getInstance(multitonKey);
}

override protected function initializeController() : void {
if ( controller != null ) return;
controller = ExtendedController.getInstance(multitonKey);
}

public function renameMediator(mediatorName : String, newName : String) : void {
ExtendedView(view).renameMediator(mediatorName, newName);
}
}

public class ExtendedView extends View {
override public function ExtendedView(multitonKey : String) {
super(multitonKey);
}

override protected function initializeView( ) : void {
instanceMap[ multitonKey ] = this;
}

public static function getInstance( key:String ) : IView {
if ( instanceMap[ key ] == null ){
var view : ExtendedView = new ExtendedView( key );
instanceMap[ key ] = view;
}
return ExtendedView(instanceMap[ key ]);
}

public function renameMediator(mediatorName : String, newName : String) : void {
var mediator : IMediator = mediatorMap[mediatorName];
if(!mediator) return;
delete mediatorMap[mediatorName];
mediatorMap[newName] = mediator;
}
}

public class ExtendedMediator extends Mediator {

public function ExtendedMediator(mediatorName : String, viewComponent : Object = null){
super(mediatorName, viewComponent);
}

public function getFacade() : ExtendedFacade {
return facade as ExtendedFacade;
}
}

public class ExtendedController extends Controller {
public function ExtendedController(multitonKey : String) {
super(multitonKey);
}

override protected function initializeController() : void {
view = ExtendedView.getInstance(multitonKey);
}

public static function getInstance( key:String ) : ExtendedController {
if ( instanceMap[ key ] == null ) instanceMap[ key ] = new ExtendedController( key );
return instanceMap[ key ];
}
}

so i have two questions
1. How bad is this solution? Is there a better one?
2. Would it cause any bugs in puremvc that i didnt notice?

thanks in advance!
« Last Edit: February 16, 2011, 12:44:09 by sirGordon » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: February 16, 2011, 01:30:29 »

I don't understand why you want to rename the mediator, which actor would invoke the renaming and when you'd be doing it.

i check facade.hasMediator(<id of the document>) which returns false

This tells me that you either...

1) failed to supply the id of the document in the call to super in your mediator subclass' constructor,
2) you created the name differently when the mediator was registered than when you check for it,
3) the id you have in hand when you're calling facade.hasMediator(<id of the document>) is not correct.
4) the mediator has already been removed by the time you're calling facade.hasMediator(<id of the document>)

If you register a mediator with a given name, facade.hasMediator will always return true if you supply it the name the mediator was registered with AND it hasn't been removed.

-=Cliff>
Logged
sirGordon
Newbie
*
Posts: 4


View Profile Email
« Reply #2 on: February 16, 2011, 11:24:10 »

exactly, Cliff! My case is number 2)
because the document does not have an id before it is saved to database, the document edit mediator name is set using UIDUtil.getUID() - as a temporary mediatorName
the problem is, when the user saves the document, i need to reset mediatorName to the newly received (from database) document id, so hasMediator(<document id>) would return "true"

upon receiving document id, mediator will change its name itself
« Last Edit: February 18, 2011, 06:40:21 by sirGordon » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #3 on: February 20, 2011, 11:43:25 »

Then do this:

1) Have the old mediator be interested in a notification which has the new id as the note body or type.
2) Handle the note by registering a new mediator with the new name, passing it the view component
3) Have the old mediator remove itself
4) Have the mediator's onRemove method remove all event listeners from the view component and set it to null, so that the old mediator will be GC'd

-=Cliff>
Logged
Pages: [1]
Print