tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/3724-ieventdispatcher-null-in-modelRobotlegs: Discussion 2013-08-30T08:00:16Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-03T23:37:54Z2013-07-03T23:37:54ZIEventDispatcher null in Model<div><blockquote>
<p>The problem is that when I try to access eventDispatcher, it is
null.</p>
</blockquote>
<p>When are you trying to access it? You can't access it in the
constructor because the property won't have been injected yet. See:
<a href=
"https://github.com/robotlegs/robotlegs-framework/wiki/common-problems#wiki-null-in-constructor">
https://github.com/robotlegs/robotlegs-framework/wiki/common-proble...</a></p>
<p>Hope that helps!</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-04T00:34:39Z2013-07-04T00:34:39ZIEventDispatcher null in Model<div><p>Wish it was THAT simple ;-)</p>
<p>I'm trying to call it after some data is updated in the model
via a setter method.</p>
<p>OtherClass -> GameModel.setData() ->
GameModel.notifyUpdate() -> EventDispatcher.dispatchEvent ->
(null object reference error on EventDispatcher).</p></div>Kyletag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-04T07:25:25Z2013-07-04T07:25:38ZIEventDispatcher null in Model<div><p>Hi Kyle,</p>
<p>I can think of 3 other possibilities in addition to what Shaun
said:</p>
<ul>
<li>
<p>you don't keep a reference to the context?</p>
</li>
<li>
<p>metadata stripping?</p>
</li>
<li>
<p>Model not injected?</p>
</li>
</ul>
<p>You mapped it like this?</p>
<p>injector.map(WeirdModel).asSingleton();</p>
<p>And your Model looks like this?</p>
<pre>
<code>public class WeirdModel
{
[Inject]
public var eventDispatcher:IEventDispatcher;
public function WeirdModel()
{
}
public function weirdMethod(value:String):void
{
eventDispatcher.dispatchEvent(new SomeEvent(SomeEvent.SOME_TYPE, value));
}
}</code>
</pre>
<p>And you injected it like this in SomeClass?</p>
<pre>
<code>[Inject]
public var weirdModel:WeirdModel;
…
weirdModel.weirdMethod("someValue");</code>
</pre>
<p>Or did you by any chance instantiate it like this?</p>
<p><strong>weirdModel = new WeirdModel();</strong> // bad</p>
<p>This wouldn’t work; The Injector won’t inject the
IEventDispatcher into that Model.</p>
<p>So, if you don’t want to inject it, then let the injector
instantiate the model:</p>
<p><strong>weirdModel = injector.getInstance(WeirdModel);</strong>
// good</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-04T08:23:43Z2013-07-04T08:23:43ZIEventDispatcher null in Model<div><p>Hey Ondina!<br>
I think number three (wierd model example) looks sort of like y<br>
implementation... What's wrong with that example? Is it the<br>
mapAsSingleton() piece or..?</p>
<p>Thanks,</p>
<p>Kyle</p></div>Kyletag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-04T08:40:41Z2013-07-04T08:40:41ZIEventDispatcher null in Model<div><p>Nah, the mapping is just fine.<br>
Are you doing this:</p>
<p>weirdModel = new WeirdModel();</p>
<p>??</p>
<p>Can you paste the code where you instantiate the model?</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-04T14:49:33Z2013-07-04T14:49:33ZIEventDispatcher null in Model<div><p>I have it mapped as a singleton, then injected into my service
.<br>
Shouldn't it be instantiated automatically when mapping as a
singleton? (I<br>
also pasted the line where I do the mapping in my original post
for<br>
reference)</p>
<p>Thanks and happy 4th!</p>
<p>-Kyle</p></div>Kyletag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-04T14:53:56Z2013-07-04T14:53:56ZIEventDispatcher null in Model<div><blockquote>
<p>Shouldn't it be instantiated automatically when mapping as a
singleton?</p>
</blockquote>
<p>Not exactly. The mapping won't instantiate it, it will only be
instantiated on the first request (Injection point).</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-04T14:57:45Z2013-07-04T14:57:45ZIEventDispatcher null in Model<div><p>If you injected it, it should work.<br>
Maybe you call the service before the mappings are done?</p>
<p>Try to call the service in a context.afterInitializing handler
and see what happens.</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-04T15:00:33Z2013-07-04T15:00:33ZIEventDispatcher null in Model<div><p>To debug this you could try creating a [PostConstruct] method on
your model, then see what happens first (injection or your setter
method). For example:</p>
<pre>
<code>public class SomeModel
{
[Inject]
public var eventDispatcher:IEventDispatcher;
[PostConstruct]
public function init():void
{
trace('SomeModel PostConstruct');
}
public function setter(value:String):void
{
trace('SomeModel setter called');
eventDispatcher.dispatchEvent(new SomeEvent(SomeEvent.SOME_TYPE, value));
}
}</code>
</pre></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-05T16:33:44Z2013-07-05T16:35:01ZIEventDispatcher null in Model<div><p>Hey Sean/Ondina,</p>
<p>So I went ahead and ran with the [PostContstruct] attempt and as
you both suspected, the model is trying to access the
eventDispatcher before [PostContstruct] is called (the model is
inject).</p>
<p>So the question becomes: Why is the model not injected yet, and
how do I ensure that the model is created and has been injected
before I run my StartupCommand (which begins loading the data that
eventually calls the method on my model that tries to access the
eventDispatcher).</p>
<p>Currently my Context / Setup routine looks like this:</p>
<pre>
<code>_context = new Context()
.install(MVCSBundle)
.install(RelaxedEventMapExtension)
.configure(ModelConfig, ViewConfig, CommandConfig, ServiceConfig, EventConfig, StartupConfig)
.configure(new ContextView(this));</code>
</pre>
<p>The last config added "StartupConfig" is what kicks off my data
loading cycle. Perhaps I should instead be listening for an event
to notify me that the context is ready to rock, and then dispatch
an event to fire off a command which houses my startup logic?
Perhaps the context.afterInitializing that Ondina mentioned?</p>
<p>Thanks!</p>
<p>-Kyle</p></div>Kyletag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-05T17:00:36Z2013-07-05T17:00:50ZIEventDispatcher null in Model<div><p>My new setup looks like this:</p>
<pre>
<code>[Inject]
public var eventDispatcher:IEventDispatcher;
public function MySuperAwesomeApp()
{
init();
}
/**
* Create the context instance which will initialze the application.
*/
private function init():void
{
_context = new Context()
.afterInitializing( startup )
.install(MVCSBundle)
.install(RelaxedEventMapExtension)
.configure(ModelConfig, ViewConfig, CommandConfig, ServiceConfig, EventConfig)
.configure(new ContextView(this));
}
private function startup():void
{
trace("STARTUP");
eventDispatcher.dispatchEvent( new ApplicationEvent( ApplicationEvent.STARTUP ) );
}</code>
</pre>
<p>After firing up, the startup method is called; however,
eventDispatcher is null. Do I need to do something else to have the
eventDispatcher inject into my root class, or is there a better way
to dispatch an event out to the framework from here?</p>
<p>Thanks!</p>
<p>-Kyle</p></div>Kyletag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-05T17:02:52Z2013-07-05T18:03:43ZIEventDispatcher null in Model<div><p>It's also worth noting that my _context reference is null when
the startup() method is called. I believe this is due to the method
chaining on the context object. If I move the calls out into their
own lines like this:</p>
<pre>
<code>_context = new Context();
_context.afterInitializing( startup );
_context.install(MVCSBundle);
_context.install(RelaxedEventMapExtension);
_context.configure(ModelConfig, ViewConfig, CommandConfig, ServiceConfig, EventConfig);
_context.configure(new ContextView(this));</code>
</pre>
<p>...the _context is available in the startup method; however,
calling dispatchEvent via the _context object still does not reach
the framework (trigger the command).</p>
<p>I also found a thread where it is mentioned that the "proper"
way to handle a startup routine such as this is to write the
startup logic in a config file (the original way I was handling
this); however, I'm not sure how this should work since the config
file is called before my actors have been injected (actors
injecting, sounds so Hollywood! ha).</p></div>Kyletag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-05T18:19:17Z2013-07-05T18:19:17ZIEventDispatcher null in Model<div><p>Ok, this is officially becoming a bit maddening.. I figured out
how to get an instance of the eventDispatcher off of the context
and dispatch an event out to the framework; however, when the data
model is reached, it still has not been injected.</p>
<p>So my flow is currently -> afterInitialization -> Disptach
event to fire off service -> Service returns data and updates
model -> Model tries to dispatch event to notify update but
fails since it has not yet been injected with the
eventDispatcher.</p>
<p>My new code is as follows:</p>
<pre>
<code>private var _context:IContext
public function MySuperAwesomeApp()
{
init();
}
/**
* Create the context instance which will initialze the application.
*/
private function init():void
{
_context = new Context();
_context.afterInitializing( startup );
_context.install(MVCSBundle);
_context.install(RelaxedEventMapExtension);
_context.configure(ModelConfig, ViewConfig, CommandConfig, ServiceConfig, EventConfig);
_context.configure(new ContextView(this));
}
private function startup():void
{
trace("STARTUP");
var eventDispatcher:IEventDispatcher = _context.injector.getInstance(IEventDispatcher);
eventDispatcher.dispatchEvent( new ApplicationEvent( ApplicationEvent.STARTUP ) );
}</code>
</pre>
<p>I would assume that when the afterInitializing function is
called that all injections should have taken place, but this is not
the case... =\</p></div>Kyletag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-05T20:28:04Z2013-07-05T20:28:04ZIEventDispatcher null in Model<div><blockquote>
<p>Service returns data and updates model</p>
</blockquote>
<p>OK, so the model is injected into the service? And you're
definitely not "newing" it up yourself anywhere? Have you put a
trace in the model's constructor to be sure that there's only
one?</p>
<blockquote>
<p>It's also worth noting that my _context reference is null when
the startup() method is called.</p>
</blockquote>
<p>This tells me that your service is actually synchronous and your
contextView object is already on stage. The context initialization
is triggered by adding a view that is already on stage, and the
whole context initialization process is completing (and calling the
callback) before the context instance has been assigned to your
field.</p>
<p>I have to run right now, but I'll try get back to this
later.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-05T22:02:07Z2013-07-05T22:02:07ZIEventDispatcher null in Model<div><p>Hey Shaun,<br>
I have verified that the Model's constructor is only being called
once, and that there are no references to "new MyModel()" anywhere
in the project. Does it seem like the way I am implementing this is
correct, or is there perhaps some other way? Should the
afterInitializing method be called AFTER all
initialization/injection has completed (still trying to understand
this piece a bit).</p>
<p>Thanks!</p>
<p>-Kyle</p></div>Kyletag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-06T23:21:10Z2013-07-06T23:21:10ZIEventDispatcher null in Model<div><p>Heya,</p>
<blockquote>
<p>Does it seem like the way I am implementing this is correct</p>
</blockquote>
<p>You first context configuration example should work. I wouldn't
pull the event dispatcher out using the afterInitializing callback
like in your last example, but really, that should work too. It
seems like there is something subtle going on here.</p>
<blockquote>
<p>Should the afterInitializing method be called AFTER all
initialization/injection has completed (still trying to understand
this piece a bit).</p>
</blockquote>
<p>It occurs after all the configs have been run and the framework
is fully initialized. That doesn't mean that all singleton objects
have been created though - only those that have been requested.</p>
<p>It's really hard to figure out what is going on here without
actual code to walk through. Could you create the simplest example
app possible that demonstrates your issue? In the meantime though:
what do your configs look like? (do they implement IConfig, or do
they make use of <code>[PostConstruct]</code>?)</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-07T05:24:05Z2013-07-07T05:24:05ZIEventDispatcher null in Model<div><p>Hey Shaun,<br>
Ill look into getting a simple example project together and see if
I can<br>
replicate the issue. My configs implant Iconfig and are not using
the<br>
postconstruct metadata.</p>
<p>From the way you're describing things it almost sounds like its
a race<br>
condition between the context creating and I jetting the model
instance<br>
when it is first requested and the command that is trying to set
the<br>
property on the model.</p>
<p>If that is the case it would seem like you would have to somehow
first<br>
ensure that the model had been injected before trying to set any
properties<br>
on it, which I would think would present a pretty major problem
(see also a<br>
common issue).</p>
<p>Any thoughts on that?</p>
<p>Thanks again for stickin with me on this one!</p>
<p>Kyle</p></div>Kyletag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-07T18:06:31Z2013-07-07T18:06:31ZIEventDispatcher null in Model<div><blockquote>
<p>Thanks again for stickin with me on this one!</p>
</blockquote>
<p>No problemo! I'm sure we'll figure it out soon. I'd be very
surprised if there was a bug in the framework that is causing this,
but it's certainly possible.</p>
<p>Have you tried installing the
<code>InjectorActivityLoggingExtension</code> to see what the
injector is getting up to? It might provide some clues. Log
everything!</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-09T13:42:57Z2013-07-09T13:42:57ZIEventDispatcher null in Model<div><blockquote>
<p>It's also worth noting that my _context reference is null when
the startup() method is called.</p>
</blockquote>
<p>I can confirm that the context is null in
afterInitializing’s handler, when the contextView is already
on stage by the time the context is created.<br>
That happens in a pure as3 project, when the context is created in
the root sprite. To avoid this, the context should be created in a
root-view’s child.</p>
<p>In Flex it is possible to create a context on preinitialize or
creation complete.</p>
<p>I saw that the StageSyncExtension is calling initialize() on the
context.<br>
If the contextView is <strong>not on stage yet</strong>,
StageSyncExtension adds an event listener for ADDED_TO_STAGE and in
its handler it calls _context.initialize();</p>
<p>If the contextView is <strong>already on stage</strong>, it
calls _context.initialize(); right away.</p>
<p>In this case the context is null in all of the handlers:<br>
.beforeInitializing(beforeInitializing),
.whenInitializing(whenInitializing),
.afterInitializing(afterInitializing)</p>
<p>The docs for the handlers say that beforeInitializing can be
asynchronous, whenInitializing must be synchronous and
afterInitializing must be synchronous.<br>
The life of a Context is sooo complicated;)</p>
<p>To see what happens if the _context.initialize(); is
<strong>delayed</strong>, I added a<br>
Timer and after that it worked as expected, i.e. the handlers had
access to the context.</p>
<p>I guess having a Timer is not a good solution though.</p>
<p>That’s also working, as Kyle has noticed:<br></p>
<pre>
<code>
_context = new Context();
_context.afterInitializing( startup )
.install(MVCSBundle)
.install(RelaxedEventMapExtension)
.configure(ModelConfig, ViewConfig, CommandConfig, ServiceConfig, EventConfig)
.configure(new ContextView(this))</code>
</pre></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-09T14:33:57Z2013-07-09T14:33:57ZIEventDispatcher null in Model<div><p>I initialize my context in the document class of my Flash Pro
project, so<br>
perhaps that is the culprit. Ondina, it sounds like the way to
resolve this<br>
is to create a child class that instantiated my context then create
an<br>
instance of that class and attach it to the displaylist in my
document<br>
class class. Does that sound about right?</p>
<p>Thanks!</p>
<p>Kyle</p></div>Kyletag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-09T14:45:42Z2013-07-09T14:45:42ZIEventDispatcher null in Model<div><blockquote>
<p>create a child class that instantiated my context then create an
instance of that class and attach it to the displaylist in my
document<br>
class class. Does that sound about right?</p>
</blockquote>
<p>Exactly!</p>
<p>@Shaun</p>
<p>ENTER_FRAME better than a Timer?</p>
<pre>
<code>StageSyncExtension
if (_contextView.stage)
_contextView.addEventListener(Event.ENTER_FRAME, initializeContextLater);
…
protected function initializeContextLater(event:Event):void
{
_logger.debug("Context view is now on stage. Initializing context...");
_contextView.removeEventListener(Event.ENTER_FRAME, initializeContextLater);
_context.initialize();
}</code>
</pre></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-09T14:46:53Z2013-07-09T14:46:53ZIEventDispatcher null in Model<div><blockquote>
<p>create an instance of that class</p>
</blockquote>
<p>It has to be a Sprite though.</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-09T15:26:42Z2013-07-09T15:26:42ZIEventDispatcher null in Model<div><p>@Kyle I’ve attached an example for you.<br>
ShellContextView is the ‘document class’<br>
SomeView is the child mediated by SomeMediator, which dispatches
SomeEvent on behalf of its view triggereing SomeCommand that
accesses the injected SomeModel, which dispatches SomeEvent with
the updated data, to which SomeMediator is listening and lets
SomeView display the data. Ah, long sentence;)</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-09T16:01:32Z2013-07-09T16:01:32ZIEventDispatcher null in Model<div><p>Thanks for the Example Ondina! I understand the reasons behind
it, but having to create my Context in another child view just
feels dirty for some reason haha. It seems like that is the
solution for now, but my brain is screaming that the Context should
be initialized inside the Document class. I'll give this a shot and
see if it resolves the issues I was seeing, will report back
shortly!</p></div>Kyletag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-09T16:24:59Z2013-07-09T16:24:59ZIEventDispatcher null in Model<div><p>You’re welcome!</p>
<blockquote>
<p>my brain is screaming that the Context should be initialized
inside the Document class</p>
</blockquote>
<p>Yes, I can empathize with what you're saying.<br>
Let’s see if Shaun agrees with what I suggested to be done in
StageSyncExtension, or if he comes up with a better solution to
this.</p>
<blockquote>
<p>I'll give this a shot and see if it resolves the issues I was
seeing, will report back shortly!</p>
</blockquote>
<p>My example works just fine. Try it out and add a service to it.
If it doesn’t work, then something is wrong with your service
class.</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-09T18:01:53Z2013-07-09T18:01:53ZIEventDispatcher null in Model<div><p>I FOUND IT (it being MY error)! So after looking over the code
about 1,000 times I finally noticed that I was initializing some
Vectors in an init method within my model. No big deal, except for
the fact that I was initializing them by creating a new Vector and
assigning it to my property setter (as opposed to the private
variable that the setter accesses). Calling the setter was causing
my notifyUpdate() method to get called which in-turn tried to
dispatch an event (with an IEventDispatcher that had not yet been
injected). So looks like this one was on me (no terrible surprise
there).</p>
<p>That being said, it seems we have raised a potential GOTCHA in
the process (creating the Context in a child Sprite for AS3 only
and Flash Pro projects). Thanks again for all the help on this one
guys!</p></div>Kyletag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-11T08:09:44Z2013-07-11T08:09:44ZIEventDispatcher null in Model<div><p>Hey @ondina,</p>
<p>what do you mean when you say</p>
<blockquote>
<p>In this case the context is null in all of the handlers:
.beforeInitializing(beforeInitializing),
.whenInitializing(whenInitializing),
.afterInitializing(afterInitializing)</p>
</blockquote>
<p>AFAIK the context isn't passed to the afterInitializing handler
at all, only an instance of the LifeCycleEvent, so I don't quite
get what 'context' you refer to?</p></div>creynderstag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-11T09:43:19Z2013-07-11T09:46:03ZIEventDispatcher null in Model<div><p>Hi creynders,</p>
<p>Welcome back :)</p>
<p>beforeInitializing, whenInitializing, afterInitializing,
beforeSuspending, whenSuspending, afterSuspending, beforeResuming,
whenResuming, afterResuming, beforeDestroying, whenDestroying,
afterDestroying, they all return an <strong>IContext</strong>.</p>
<p><strong>Context.as</strong></p>
<pre>
<code>public function afterInitializing(handler:Function):IContext
{
_lifecycle.afterInitializing(handler);
return this;
}</code>
</pre>
<p>When the contextView is already on stage, which happens when you
create the context in the document class of a as3 project or in
Flex onAddedToStage, the _context.initialize(); runs immediately
inside of StageSyncExtension.handleContextView(). I don’t
quite understand what’s happening there, but the lifecycle
handlers seem to have some issues ( sync / async ???).</p>
<p><strong>StageSyncExtension</strong></p>
<pre>
<code>private function handleContextView(contextView:ContextView):void
{
if (_contextView)
{
_logger.warn('A contextView has already been installed, ignoring {0}', [contextView.view]);
return;
}
_contextView = contextView.view;
if (_contextView.stage)
{
initializeContext();
}
else
{
_logger.debug("Context view is not yet on stage. Waiting...");
_contextView.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
}
private function initializeContext():void
{
_logger.debug("Context view is now on stage. Initializing context...");
_context.initialize();
_contextView.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}</code>
</pre>
<p>If the context initialization takes place in a Flex
preinitialize or creationComplete handler, or in a sub-view of a
document class in as3, StageSyncExtension waits for the view to be
added to the stage before initializing the context, and the
lifecycle handlers return an IContext as expected.</p>
<p>Not sure if this is <strong>the</strong> solution, but after
delaying the context initialization inside of
StageSyncExtension.handleContextView by using a timer or this:</p>
<pre>
<code>
if (_contextView.stage)
_contextView.addEventListener(Event.ENTER_FRAME, initializeContextLater);</code>
</pre>
<p>the lifecycle handlers return an IContext even if the
contextView is already on stage.</p>
<p>Maybe the culprit is somewhere else, but I can’t find it,
and I don’t know what tests should I run to catch it.</p>
<p>Context creation and configuration and the handlers that should
return an IContext:</p>
<pre>
<code>private var _context:IContext;
public function ShellContext(view:DisplayObjectContainer)
{
_context = new Context()
.beforeInitializing(beforeInitializing)
.whenInitializing(whenInitializing)
.afterInitializing(afterInitializing)
.install(MVCSBundle)
.configure(MediatorsConfig, ModelsConfig, ControllersConfig)
.configure(new ContextView(view));
}
private function beforeInitializing():void
{
trace(".beforeInitializing()" + _context);
}
private function whenInitializing():void
{
trace(".whenInitializing()" + _context);
}
private function afterInitializing():void
{
trace(".afterInitializing()" + _context);
}</code>
</pre></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-11T11:36:19Z2013-07-11T11:36:19ZIEventDispatcher null in Model<div><p>The reason why all the traces in the handlers return null is
because they're called before the context instance is stored in the
_context property. This is one of the subtleties we need to be
aware of when calling method chains that execute synchronously.</p>
<p>I.e., it doesn't have anything to do with the context view
already being on the stage. This should solve all problems (no need
to initialize the context in a sub-view etc.)<br>
just store the context instance in a property, BEFORE starting the
method chaining:<br></p>
<pre>
<code> _context = new Context();
_context.beforeInitializing(beforeInitializing)
.whenInitializing(whenInitializing)
.afterInitializing(afterInitializing)
.install(MVCSBundle)
.configure(MediatorsConfig, ModelsConfig, ControllersConfig)
.configure(new ContextView(view));</code>
</pre>
<p>As a rule of thumb, I never start chaining immediately after
instantiation exactly to avoid this pitfall. Probably we should
point this out somewhere in the documentation.</p></div>creynderstag:robotlegs.tenderapp.com,2009-10-18:Comment/276191682013-07-11T12:13:53Z2013-07-11T12:13:53ZIEventDispatcher null in Model<div><blockquote>
<p>just store the context instance in a property, BEFORE starting
the method chaining:</p>
</blockquote>
<p>Yes, that’s already been discussed in the previous posts.
[<a href=
"http://knowledge.robotlegs.org/discussions/robotlegs-2/3724-ieventdispatcher-null-in-model#comment_27688000">http://knowledge.robotlegs.org/discussions/robotlegs-2/3724-ieventd...</a>]</p>
<blockquote>
<p>it doesn't have anything to do with the context view already
being on the stage.</p>
</blockquote>
<p>Ok, then we don’t need the StageSyncExtension at all.<br>
We could do just this:</p>
<pre>
<code>public function MinimalistContext(view:DisplayObjectContainer)
{
_context = new Context()
.afterInitializing(afterInitializing)
.install(ContextViewExtension)
.install(ViewManagerExtension)
.configure(ContextViewListenerConfig)
.configure(new ContextView(view))
_context.initialize();
}</code>
</pre>
<blockquote>
<p>Probably we should point this out somewhere in the
documentation.</p>
</blockquote>
<p>Oh yes, we should!!!</p></div>Ondina D.F.