Injector seems to always create new view instance
I have a view that continuously gets removed and added to the stage. It appears that each time I add it to the stage a new instance is being created and also a different mediator might be being created. Somehow though, multiple mediators view listeneres are being triggered and each time the view is added I get more and more traces from these methods.
I have mapped KeysView in my Bootstrap command using:
injector.mapClass(KeysView, KeysView);
I have mapped the KeysView's mediator like so:
mediatorMap.mapView(KeysView, KeysViewMediator);
To create the KeysView I use this code in a DisplayViewCommand
after determining which view to display
view = injector.getInstance(KeysView);
contextView.addChild(view); (which should trigger the mediator
being added for it.
I thought getInstance() would create the class one time, and then the next time would retrieve that same instance of the class, is that correct/ Because it doesn't seem to be happening that way :/
Thanks!
Kyle
Comments are currently closed for this discussion. You can start a new one.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
1 Posted by kamcknig on 24 Feb, 2012 04:20 PM
OK, I've found why it's creating a new view instance each time.
I changed the class mapping to
injector.mapSingleton(KeysView);
This started to create only the single instance of the view. But it seems as though I'm still getting different instances of the mediator created, and therefore too many event handlers are being fired.
2 Posted by Stray on 24 Feb, 2012 04:22 PM
How are you adding your listeners in onRegister of the mediator?
Are you using the eventMap? If not, the events won't get cleaned up when the mediator is killed off (which should happen automatically when the view leaves the stage).
Stray
3 Posted by kamcknig on 24 Feb, 2012 04:25 PM
addViewListener(BeatMakerEvent.DISPLAY_MAIN_VIEW, dispatch, BeatMakerEvent);
4 Posted by Stray on 24 Feb, 2012 04:29 PM
And are you unmapping those relaxedEventMap listeners in onRemove() ?
See the section on clean up in the ReadMe here:
https://github.com/Stray/robotlegs-utilities-RelaxedEventMap
5 Posted by kamcknig on 24 Feb, 2012 04:40 PM
OK, so now I'm still getting a new instance of the mediator. But it appears that the old instances are being cleaned up properly. So this is how it is suppsoed to be correct? But I do still have one issue.
My handler for my "onTypeSet" event gets called once the first time, but then gets called twice every time after that (though it doesn't continue to grow in call counts like it used to).
I am adding it to the relaxed event map and the mediator's event map, could that be why? But if so why does it only call it once the first time?
Kyle
6 Posted by kamcknig on 24 Feb, 2012 04:40 PM
Yep, that was why. If i remove the listener from the mediator's event map and only use the relaxed one it always calls it once.
Kyle
7 Posted by Stray on 24 Feb, 2012 04:42 PM
I'm guessing that the first time round, the event has actually happened just before the mediator runs onRegister - so only the relaxed one is getting run. After that, both get run. You don't need to add that listener twice - the relaxed one will cover all the future happenings as well as the past one.
8 Posted by kamcknig on 24 Feb, 2012 04:48 PM
Great, thanks so much! Love this forum, you are always on top of everything!
kamcknig closed this discussion on 24 Feb, 2012 04:48 PM.