Forcing a resize event once a background image has loaded

Sean's Avatar

Sean

31 Aug, 2011 01:42 PM

Hi, i'm new to this, and fairly new to using an MVC structured project in general to be honest.
What would be the best practise for generating a resize event after a bgd image has loaded in one of my views?
I have a ResizeModel, which has been mapped in the main context using injector.mapSingleton(), i initialise this passing it a reference to the stage, the model then listens for RESIZE events and dispatches a custom event with the stage's width&height packaged into the event.
I have added a context listener to the mediator associated with the background image which picks the stage resize events up and handles the image resizing accordingly. The problem is i need to be able to fire this resize event (via the ResizeModel) after the bgd image has loaded.
What is the best way to force this event from the model from within a mediator whilst still maintaining decoupled objects? Via a command?

I know this is probably a standard process in the MVC pattern but just want to make sure i start off correctly :)

Thanks
Sean

  1. 1 Posted by pavel.fljot on 31 Aug, 2011 02:01 PM

    pavel.fljot's Avatar

    I don't think it's a good idea to fire some "fake" event since (at least) there can be other members who could respond to it and make some useless complex calculations. It's better for model to keep size so you could get them when needed and apply them to your background image when needed.

  2. 2 Posted by Sean on 31 Aug, 2011 02:13 PM

    Sean's Avatar

    Thanks for the reply, so are you saying i should inject the ResizeModel into the BgdViewMediator instance, and then just grab the stage w/h data off it once the image has loaded? I know it's a bit controversial to reference the models directly from the mediators isn't it?
    I was thinking about sending a message to the model to tell it to send a message back with the latest stage data is that a better way of doing it or just a different way?
    Sorry to be pedantic about it, i'm just very new to the framework and want to make sure i'm doing it the best way for the long term so as to keep things decoupled.

    Would you suggest to inject the Model and reference it directly in the mediator?

    Thanks again

  3. 3 Posted by pavel.fljot on 31 Aug, 2011 02:22 PM

    pavel.fljot's Avatar

    Yes, I would inject model into mediator in this case. Can't guarantee though that gurus would do the same =) You can wait for their answer.

  4. 4 Posted by Sean on 31 Aug, 2011 02:34 PM

    Sean's Avatar

    I have tried it both ways now, and certainly the injection method is the easiest and most elegant way of the two :)
    I just wanted to make sure i wasn't making some fatal error which could lead to major issues later on.

    Cheers Pavel

  5. 5 Posted by pavel.fljot on 31 Aug, 2011 02:54 PM

    pavel.fljot's Avatar

    Well I mentioned one most (for me) obvious and expensive side-effect. Imagine you have several other something (components/actors/commands/whatever) that listen to that same resize event. So when you force to fire it — all of them will react instead of background only. Not nice IMHO.
    You could think of others...

  6. Support Staff 6 Posted by creynders on 01 Sep, 2011 06:17 PM

    creynders's Avatar

    I'm certainly no guru, but maybe my 2 cents can help out as well.
    In general I let views respond to the stage resize event directly. It IS a view event and all display objects have a direct reference to the stage, so I see no need to drag the entire framework into it.
    However, if complex calculations need to be made or multiple views need the same coordinates for instance then I do rely on a StageModel. More often than not this means it maintains a set of coordinates, registration points that several views need to adjust their position to. In that case the update cycle is pretty straight-forward too. The ContextViewMediator picks up the stage resize event and either relays it to the framework or dispatches a custom event. A command picks it up and updates the StageModel, which decides whether the dispatched data is relevant and updates its data if necessary. The model sends out some kind of STAGEDATA_UPDATE event with a VO which gets picked up by the various mediators that pass the relevant data to their views.
    To solve the problem of mediators "missing" the event because they are not registered yet I use the RelaxedEventMap utility:
    https://github.com/Stray/robotlegs-utilities-RelaxedEventMap

  7. 7 Posted by Sean on 02 Sep, 2011 10:08 AM

    Sean's Avatar

    After a couple of days experimenting with the framework (i'm a bit slow;) i'm starting to get a grip on the event flow process and the different ways RL can be used to update views ( via the model, commands, injection etc).
    Thanks for your input on this - the link for the RelaxedEventMap will certainly come in useful.

  8. Ondina D.F. closed this discussion on 02 Nov, 2011 04:00 PM.

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