Injector seems to always create new view instance

kamcknig's Avatar


24 Feb, 2012 04:14 PM

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 :/


  1. 1 Posted by kamcknig on 24 Feb, 2012 04:20 PM

    kamcknig's Avatar

    OK, I've found why it's creating a new view instance each time.

    I changed the class mapping to

    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. 2 Posted by Stray on 24 Feb, 2012 04:22 PM

    Stray's Avatar

    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).


  3. 3 Posted by kamcknig on 24 Feb, 2012 04:25 PM

    kamcknig's Avatar

    addViewListener(BeatMakerEvent.DISPLAY_MAIN_VIEW, dispatch, BeatMakerEvent);

            addViewListener(ViewChangeEvent.REMOVE_VIEW, dispatch);
            addViewListener(SoundKitEvent.SOUND_KIT_SELECTED, dispatch, SoundKitEvent);
            addViewListener(BeatMakerEvent.PLAY_SAMPLE, onPlaySample, BeatMakerEvent);
            addViewListener(ScreenEvent.TOGGLE_BLACKOUT_SCREEN, dispatch, ScreenEvent);
            addViewListener(ScreenEvent.TOGGLE_FULL_SCREEN, dispatch, ScreenEvent);
            addViewListener(BeatMakerEvent.CHANGE_TRACK_HIGH_AMOUNT, dispatch, BeatMakerEvent);
            addViewListener(BeatMakerEvent.CHANGE_TRACK_MID_AMOUNT, dispatch, BeatMakerEvent);
            addViewListener(BeatMakerEvent.CHANGE_TRACK_LOW_AMOUNT, dispatch, BeatMakerEvent);
            addViewListener(BeatMakerEvent.CHANGE_TRACK_PAN_AMOUNT, dispatch, BeatMakerEvent);
            addViewListener(BeatMakerEvent.SET_TRACK_FREQUENCY, dispatch, BeatMakerEvent);
            addViewListener(BeatMakerEvent.SET_TRACK_LFO_AMOUNT, dispatch, BeatMakerEvent);
            addViewListener(BeatMakerEvent.SET_TRACK_LFO_RATE, dispatch, BeatMakerEvent);
            addViewListener(BeatMakerEvent.SET_TRACK_RESONANCE, dispatch, BeatMakerEvent);
            addViewListener(BeatMakerEvent.IMPORT_KIT, onImportKit, BeatMakerEvent);
            addViewListener(VolumeEvent.SET_VOLUME, dispatch, VolumeEvent);
            addViewListener("typeSet", dispatch, Event);
            relaxedEventMap.mapRelaxedListener("typeSet", onTypeSet, Event);
            relaxedEventMap.mapRelaxedListener(BeatMakerEvent.MASTER_VOLUME_SET, onMasterVolumeSet, BeatMakerEvent);
            addContextListener(BeatMakerEvent.MASTER_VOLUME_SET, onMasterVolumeSet, BeatMakerEvent);
            addContextListener(SoundKitEvent.SOUND_KIT_SELECTED, onSoundKitSelected, SoundKitEvent);
            addContextListener(BeatMakerEvent.SOUND_KIT_IMPORT_SUCCESSFUL, onSoundKitImported, BeatMakerEvent);
  4. 4 Posted by Stray on 24 Feb, 2012 04:29 PM

    Stray's Avatar

    And are you unmapping those relaxedEventMap listeners in onRemove() ?

    See the section on clean up in the ReadMe here:

  5. 5 Posted by kamcknig on 24 Feb, 2012 04:40 PM

    kamcknig's Avatar

    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?


  6. 6 Posted by kamcknig on 24 Feb, 2012 04:40 PM

    kamcknig's Avatar

    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.


  7. 7 Posted by Stray on 24 Feb, 2012 04:42 PM

    Stray's Avatar

    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. 8 Posted by kamcknig on 24 Feb, 2012 04:48 PM

    kamcknig's Avatar

    Great, thanks so much! Love this forum, you are always on top of everything!

  9. kamcknig closed this discussion on 24 Feb, 2012 04:48 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? 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