Can I access a Model from a View?

Gert-Jan van der Wel's Avatar

Gert-Jan van der Wel

13 Oct, 2011 09:13 AM

I'm working on a drawing application. I keep my drawing logic, like drawing a shape, in a View. When a new shape has been drawn, it is added to a Model (by a Command triggered by an event sent by a Mediator).

When drawing a new shape, I want to be able to snap to other shapes on the view. To calculate snap coordinates I need the data from my Model. I don't think it's wise to inject my model into my view.

Should I restructure my app?

  1. 1 Posted by Pierre Laveklin... on 13 Oct, 2011 09:30 AM

    Pierre Laveklint's Avatar

    You could inject the model in your view Mediator

    [Inject]
    public var drawingModel:YourModel
    

    but you could also dispatch an Event with coordiantes as an argument from your Model each time a new shape has been added to your Model. Make your view Mediator listen to that event.

  2. 2 Posted by Peder on 13 Oct, 2011 09:57 AM

    Peder's Avatar

    Or you could pass the reference from the mediator to the view, and access the model directly from the view (but Pierre's suggestion is the more "correct" one).

  3. 3 Posted by Gert-Jan van de... on 13 Oct, 2011 10:07 AM

    Gert-Jan van der Wel's Avatar

    Thanks for the replies! What about adding a ViewController and inject the Model into it to handles the logic?

  4. 4 Posted by Peder on 13 Oct, 2011 10:26 AM

    Peder's Avatar

    Yep, it's a good idea to add the model calls in a command class and hook an event to fire the command. Update the model in the command and dispatch a change of state event from the model which you can listen to from any other application tier - mvc pattern at it's best. :)

  5. 5 Posted by simon on 13 Oct, 2011 10:30 AM

    simon's Avatar

    Something to throw into the equation: http://www.1ndivisible.com/?p=9 as a possible option....

  6. 6 Posted by Gert-Jan van de... on 13 Oct, 2011 11:06 AM

    Gert-Jan van der Wel's Avatar

    @simon correct me if i'm wrong, but did you do this to have Presenters for your Views so you could have the logic in the Presenter instead of the View itself?

  7. 7 Posted by simon on 13 Oct, 2011 12:06 PM

    simon's Avatar

    Yes following the train of thought of PM but not the exact implementation. I
    thought I would just suggest it to mull over :)

  8. Ondina D.F. closed this discussion on 21 Nov, 2011 09:13 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