tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/816-rl2-popup-mappingsRobotlegs: Discussion 2018-10-18T16:35:45Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/247136582013-02-08T10:34:38Z2013-02-08T10:34:38ZRL2 PopUp Mappings<div><p>Hi Barjawi,</p>
<p>If it’s just a view without mediator, try this:<br></p>
<pre>
<code>var popUpView:PopUpView=injector.getOrCreateNewInstance(PopUpView);
injector.map(PopUpView).toValue(popUpView);</code>
</pre>
in your config, and then inject PopUpView into the command.
<p>I use it like this for a mediated popUpView:<br>
Config:<br></p>
<pre>
<code>mediatorMap.map(PopUpView).toMediator(PopUpViewMediator);</code>
</pre>
<p>Command:</p>
<pre>
<code>var titleWindowInstance: PopUpView = injector.getOrCreateNewInstance(PopUpView);
viewManager.addContainer(titleWindowInstance);
PopUpManager.addPopUp(titleWindowInstance, FlexGlobals.topLevelApplication as DisplayObject);
PopUpManager.centerPopUp(titleWindowInstance);</code>
</pre>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/247136582013-02-08T20:52:30Z2013-02-08T20:52:30ZRL2 PopUp Mappings<div><p>Hi Barjawi,</p>
<p>The problem is probably this line:</p>
<pre>
<code>injector.map( SkillGoalDialog ).asSingleton();</code>
</pre>
<p>If you have a mediator mapped to that class there will be a
conflict. It's generally a bad idea to map views as singletons. Is
there a reason that you need to do that?</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/247136582013-02-08T22:25:34Z2013-02-08T22:25:34ZRL2 PopUp Mappings<div><p>Thank you Shaun and Ondina for your replies.</p>
<p>I am using behaviors in my project. So I have the following
mappings in the configuration commands:</p>
<pre>
<code>mediatorMap.map( ISkillGoalItemSaveHandler ).toMediator( SkillGoalItemSaveHandlerMediator );
injector.map( SkillGoalDialog ).asSingleton();</code>
</pre>
<p>and then the dialog itself implements the
ISkillGoalItemSaveHandler.</p>
<p>I had to map the SkillGoalDialog as a singleton so that inside
the ShowSkillGoalDialogCommand I can inject it, and pop it up.</p>
<p>Now inside the dialog itself, I have two other views, both of
which implement other behaviors as well.</p>
<p>I was mistaken above to say that the dialog get its mediator to
work but the inner views that have problems. I just found out that
neither the dialog nor the inner views get their mediators to run
when I remove the "addContainer" function.</p>
<p>Thanks for all the help,</p></div>mbarjawitag:robotlegs.tenderapp.com,2009-10-18:Comment/247136582013-02-08T22:28:26Z2013-02-08T22:28:26ZRL2 PopUp Mappings<div><p>I just want to mention that this error used to occur with the
previous RL2 version, however it was always logged, and I ignored
it because everything seems to be working fine.</p>
<p>However, with the new version of RL2, the VigilanceExtension is
throwing an error because of this logged error.</p>
<p>I always wanted to know why this error shows up, but didn't have
time to post about it. :) now it came back to me.</p></div>mbarjawitag:robotlegs.tenderapp.com,2009-10-18:Comment/247136582013-02-10T05:25:32Z2013-02-10T05:25:32ZRL2 PopUp Mappings<div><p>I created a test project to test this problem. Please take a
look at <a href=
"https://github.com/MBarjawi/RL2DialogMapping">RL2DialogMapping</a>.
This project is compiled using Flash Builder 4.7, Apache Flex 4.9,
the latest RL2 (in master branch), as3-Signals, and the latest
SignalCommandMap.</p>
<ul>
<li>You will see two comments, <code>// Test 1</code> and <code>//
Test 2</code> in two files: <code>RL2DialogMappingConfig.as</code>
and <code>RL2DialogMapping.mxml</code>.</li>
<li>Currently <code>// Test 2</code> is uncommented in both
files.</li>
<li>If you run the application, then click the button to show the
popup, an error will show that is similar to the error mentioned in
the first post of this thread.</li>
<li>Click on "continue" on the Flash Player Error dialog, the
dialog will still show up.</li>
<li>If you close the dialog, then click on the button again to open
the popup again, you will get a different error stating that the
<code>dialog</code> injection is missing and the command cannot be
called.</li>
<li>This second error is very strange to me because the
<code>dialog</code> is mapped as singleton, and there is no place
in the code where I remove the mapping. So I guess it is the
<code>viewManager</code> that does this, but I am not sure.</li>
</ul>
<p>So this is in short my problem. I hope someone can help me
figure out what am I doing wrong here.</p>
<ul>
<li>Now, if you comment <code>// Test 2</code> lines, and uncomment
<code>// Test 1</code>, and try again, everything will work
smoothly.</li>
</ul>
<p>I don't like the method used in <code>// Test 1</code> because
you have to create a new instance of the dialog every time it is
opened. I want this to work in a way that the dialog is created one
time, then used as much as required. One of my dialogs is big,
opening it for the first time is a bit slow. Having it in memory
and opening it for second time, it opens much smoother.</p>
<p>Come to think about it, I don't know if keeping a dialog in
memory is a good idea or not. But this is what I have been doing so
far.</p>
<p>I am open to any recommendations and best practices, so please
let me know which way is the preferred way of opening dialogs in
RL2.</p>
<p>Finally, any comments about the way I structure the project are
also welcome. Its a quick project, but I made sure to prepare it in
the way I always use for my projects.</p>
<p>Thanks for the help.</p></div>mbarjawitag:robotlegs.tenderapp.com,2009-10-18:Comment/247136582013-02-10T08:42:13Z2013-02-10T08:42:13ZRL2 PopUp Mappings<div><p>Barjawi,<br>
I'm taking a look at your code now.</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/247136582013-02-10T12:06:41Z2013-02-10T12:06:41ZRL2 PopUp Mappings<div><p>Sorry for the delay, but, you now, it’s Sunday…and
time flows differently than during the week :)</p>
<p>As Shaun pointed out, it might not be a good idea to map a view
as a singleton.</p>
<p>I don’t know if it is what you want, but you could do
this, if you really wanted to:</p>
<p>In the config: _injector.map( MyDialog ).asSingleton();</p>
<p>ShowMyDialogCommand</p>
<pre>
<code>[Inject]
public var viewManager:IViewManager;
[Inject]
public var dialog:MyDialog;
[Inject]
public var contextView:ContextView;
[Inject]
public var injector:Injector;
public function execute():void
{
injector.unmap(MyDialog);//<====unmap
viewManager.addContainer( dialog );
PopUpManager.addPopUp( dialog, contextView.view );
PopUpManager.centerPopUp( dialog );
injector.map(MyDialog).asSingleton();//<====re-map
}</code>
</pre>
<p>Let me know if this is working for you and if it is what you
needed.</p>
<p>More explanations later today, or tomorrow..</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/247136582013-02-10T14:19:41Z2013-02-10T14:19:41ZRL2 PopUp Mappings<div><p>Hi Barjawi,</p>
<p>To understand what's going wrong you need to understand how the
framework deals with Mediators (the same concept applies to
Commands btw).</p>
<p>If you map TypeX to MediatorX, and then add an instance of TypeX
to the stage, the mediator map does this (roughly):</p>
<pre>
<code>1. Map TypeX to the actual instance that landed on stage
2. Use the injector to create an instance of MediatorX
3. Unmap TypeX from the instance that landed on stage</code>
</pre>
<p>Step 1 exists so that the view instance is available for
injection into the mediator. Step 3 is there to clean up that
mapping.</p>
<p>This why you're running into problems. When step 1 runs there is
a mapping override. When step 3 runs the mapping is destroyed.</p>
<p>Mapping your view as a singleton creates an unreasonable
situation. You're saying that there should only be a single
instance of TypeX, but that cannot be enforced. Anyone could add a
different instance of TypeX to the stage. If we did not perform the
steps above, you could imagine this happening:</p>
<pre>
<code>1. The singleton mapping enforces a single instance of TypeX. Let's call that instance `viewA`.
2. A different instance is added to the stage. Let's call that instance `viewB`
3. A mediator is created for `viewB`, but instead of being injected with `viewB`, it is injected with `viewA`.</code>
</pre>
<p>This is why you shouldn't map view components into the injector.
View instances (by their nature) are not singletons.</p>
<p>If you want to cache your view as a Singleton (big S), then
there is a more compatible way to do that. Instead of mapping it as
singleton in the injector, create a static variable somewhere:</p>
<pre>
<code>dialog = DialogCache.getSkillGoalDialog();</code>
</pre>
<p>The DialogCache might look like this (pseudo-code):</p>
<pre>
<code>class DialogCache
private static var:skillGoalDialog;
public function getSkillGoalDialog():SkillGoalDialog
return skillGoalDialog ||= new SkillGoalDialog();</code>
</pre>
<p>Hope that clears things up a little.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/247136582013-02-11T23:08:56Z2013-02-11T23:08:56ZRL2 PopUp Mappings<div><p>Thank you all for your help. I appreciate it so much.</p>
<p>I think you are correct. I shouldn't be having those dialogs as
singletons. I fixed this by creating a new instance of the dialog
each time the command is called to open the dialog.</p>
<p>Thanks for the help,</p></div>mbarjawi