Injecting a model in a view. is it ok? any alternatives?

nickt's Avatar


16 Apr, 2012 06:41 PM

Hey guys, sorry if this has been answered a million times but I'm stuck on whats best.
I have a view (RoomsView) and a model (RoomsModel). The model contains some filtering and update methods, nothing strange.
Each time there is an update, the model is dispatching an event. So, I'm guessing that you would suggest dispatching some kind of payload if I want access to the model's data from the view but I was considering the option of injecting the model to the view or it's mediator. How does that sound? any disadvantages I should consider?

  1. 1 Posted by Jerome Maurey-D... on 17 Apr, 2012 12:48 AM

    Jerome Maurey-Delaunay's Avatar


    I was struggling with the same issues and decided to adopt a simple routine. I did not create this, it was suggested by a fellow developer.

    I build each view or component of an app with the following package:
    - MyView (Sprite or other display object) - MyViewModel (Actor) - MyViewMediator (Mediator) - MyViewEvent (Event)

    The view exposes the necessary functions to be updated by the mediator as needed. If the data is complex, I pass Value Objects. It has no awareness of either the Model or the Mediator.

    The Mediator usually has 2 injections the View and the Model. Its job it to update the view with the data from the model. Or update the model with the data from the view and tell the app it has been updated. It registers listeners for the various events it handles and dispatches events as needed. All these are defined in the MyViewEvent class. The mediator has no concept of the rest of the app.

    The Model is updated by the Mediator, following user input in the view, or via Commands by other actors in the app.

    Events do not carry any data. Usually an "update" event would tell the Mediator to update the View with Model data. And a "change" event is dispatched by the Mediator when the View has changed.

    The flow is simple App Event > Execute Command > Update Model >Dispatch Event > Mediator Event Handler > Update View. Or View Signal > Mediator updates Model > Mediator DIspatches Event > App Executes Command > ...

    This tends to be boilerplate code heavy, since I creates 4 classes, minimally, per view. But I find this regimented mode to be quite flexible. A view components ends up being 100% self contained and can be poped into another app with little or no re-write.

    It also clearly segment the roles and responsibilities of the various actors. I find that managing the flow of data can be tricky, especially when many actors start firing events and trigger actions, keeping track of who does what can be hell.

    My last project ended up having over 4000 lines of code written in 2 weeks with quite a few moving parts (2D overlays, Away3D content and dynamic data) and without this structure I wouldn't be able to manage. Might not work for everything, but so far it served me well :)

    Hope this helps!



  2. 2 Posted by Paul Robertson on 17 Apr, 2012 12:59 PM

    Paul Robertson's Avatar

    As Joel likes to say, "it's about 'appropriate coupling' not 'no coupling'"

    I've used models injected into views. Even when I'm doing that I prefer to
    treat the models as "read only" and use other mechanisms to set values on
    the models. Once I even created a read-only interface for the models that I
    used for mediator injection, just to enforce it. That did end up being a
    bit more complicated from the mapping side because you can't just use
    "injector.mapSingletonOf()" (since you really need two mappings per model
    -- one read-only and one writeable).

    However, I have also seen projects that end up with a lot of tangled code
    that's hard to follow, that would not have been possible if we weren't
    injecting models into mediators. For me personally, I've decided that the
    benefit of having fewer commands and events isn't worth the risk of
    complicated mediators. But that's just what works best for me.


  3. 3 Posted by Stray on 17 Apr, 2012 11:43 PM

    Stray's Avatar


  4. 4 Posted by nickt on 19 Apr, 2012 11:35 AM

    nickt's Avatar

    Thanks a lot guys. For this specific scenario I think I will inject the model to the mediator. :)

    Till next time...

  5. Ondina D.F. closed this discussion on 19 Apr, 2012 02:24 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