How do I initialize a view without a model?

inquisitor's Avatar

inquisitor

21 Jan, 2013 04:06 AM

I've read many posts and the most recommended way is to not inject the Model into the View, but instead have the Mediator provide the data to the View in onRegister(), or for even more decoupling have the Mediator fire a request event A, process it with some Command, fire another response event B, catch it in the Mediator and put the data into the View.

I have no issues with that approach (could be a bit of a boilerplate code), but my question is: what if the View needs the model data just to initialize itself, before the mediator is even instantiated? For example, say I am writing a board game and the size of the board (8x8 or 9x9) is stored in the Model. Ideally, I want to have access to that configuration as soon as my View's addedToStage() handler is invoked, because that's where all the UI initialization is happening. Getting the board size after the view has been initialized and potentially creating more UI elements in the callback from the Mediator feels rather weird. If we were in a multi-threaded environment it would mean that the View might have to display itself while not knowing what to display.

Appreciate your thoughts.
Thanks.

  1. Support Staff 1 Posted by Ondina D.F. on 21 Jan, 2013 12:14 PM

    Ondina D.F.'s Avatar

    Hi inquisitor,

    For rl 1, you might want to look at the links Neil provided in his answer:
    http://knowledge.robotlegs.org/discussions/questions/1228-binding-i...

    Robotlegs 2 offers more solutions to this kind of problems than Robotlegs 1.

    See: https://github.com/robotlegs/robotlegs-framework/tree/master/src/ro...

    and the readme file for inspiration:
    https://github.com/robotlegs/robotlegs-framework/blob/master/src/ro...

    One of the many ways of using the ViewProcessorMapExtension:

    For example, you could inject a class containing some config values (rectWidth, rectHeight) into the View.

    In your context config class:

    injector.map(SomeVO).asSingleton();

    _viewProcessorMap.map(ViewToBeInjected).toInjection();

    In ViewToBeInjected:
    [Inject] public var someVO:SomeVO;

    graphics.drawRect(0, 0, someVO.rectWidth, someVO.rectHeight);

    or

    in your context config:
    const expectedViewWidth:Number = 100;
    const expectedViewHeight:Number = 200;

    injector.map(Number, 'rectHeight').toValue(expectedViewHeight);
    injector.map(Number, 'rectWidth').toValue(expectedViewWidth);

    _viewProcessorMap.map(ViewToBeInjected).toInjection();

    In the View:

    [Inject(name="rectWidth")] public var rectWidth:Number;

    [Inject(name="rectHeight")] public var rectHeight:Number;

    graphics.drawRect(0, 0, rectWidth, rectHeight);

    Does this help?

    Ondina

  2. 2 Posted by inquisitor on 21 Jan, 2013 06:40 PM

    inquisitor's Avatar

    Hi Ondina,

    Thank you for the links. I am already using the Presentation Model pattern, because I dislike the chaotic nature of global events. I prefer to work against well-defined interfaces, which are just as good for separation of concerns, but much better for compile time safety, coding (IDE support), and testing.

    If I understand your suggestion correctly, using the View Processors follows pretty much the same life cycle as I described in the first paragraph of my original post, only without additional events and with no boilerplate code. That sounds pretty useful. I am not sure if I can use that at the moment, because I am using Starling for my project, with s9tpepper's plugin for RL, which may only work with RL-1.

    Is the target release date for RL-2 known?

  3. Support Staff 3 Posted by Ondina D.F. on 22 Jan, 2013 09:59 AM

    Ondina D.F.'s Avatar

    Hey inquisitor,

    Yes, ViewProcessorMap is pretty flexible. Take a look at the very simple example I posted yesterday in another post:

    http://knowledge.robotlegs.org/discussions/robotlegs-2/752-guards-a...

    Regarding Robotlegs 2 and Starling:

    https://github.com/Vj3k0/robotlegs-extensions-SARS

    https://github.com/alebianco/StarlingExtension-example

    https://github.com/jamieowen/robotlegs2-starling-viewmap

    Is the target release date for RL-2 known?

    Oh, that’s a hard to answer question! ;-)

    Here, Shaun’s answer on Twitter:
    “It'll be released when it's been thoroughly reviewed: https://github.com/robotlegs/robotlegs-framework/issues/99”

    In fact, everyone who’s trying out RL2 and is writing about encountered rl2 issues/problems on this forum is also contributing to that reviewing process! Thus, I encourage you to start using RL2 :)

    Cheers,
    Ondina

  4. 4 Posted by inquisitor on 23 Jan, 2013 03:50 AM

    inquisitor's Avatar

    Thanks again, Ondina. I will checkout the SARS extension (however disturbing the name might be), as it seems to be most recently maintained.

  5. Support Staff 5 Posted by Ondina D.F. on 23 Jan, 2013 09:49 AM

    Ondina D.F.'s Avatar

    My pleasure!

    Hehe, I agree, SARS is a disturbing acronym. I don’t know whether Vjekoslav was intentionally trying to scare us, but since only Robotlungs would be affected by an infection, we shouldn’t care: P

    Have fun with Robotlegs 2 and Starling!

  6. Ondina D.F. closed this discussion on 23 Jan, 2013 09:49 AM.

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