need to know about using viewmodels

itsmylifesoham's Avatar

itsmylifesoham

13 Feb, 2012 04:35 PM

Hi,

I am using robotlegs mvc framework and have some problem. I do not want to directly bind my domain model to my view.
for eg: my domain model may be two numbers returned by a service(a,b). but to my view i want to display a+b .
which part should this logic go into? if the service gets two new numbers it will overwrite the domain model with two new numbers (x,y)
this should be reflected into my view as x+y istead of a+b. now my question is, i know that my model will extend actor and dispatch an event "numberschanged" to the view's mediator, but where will this logic of "+" operator be performed? i suppose not in the view, also not in the mediator.

I have read about using viewmodels in this case where the data the model holds is not directly bindable to the information that view displays. is this the right way to do for robotlegs? i havent seen much information on viewmodels with robotlegs.

  1. 1 Posted by bulupe on 13 Feb, 2012 06:03 PM

    bulupe's Avatar

    Hi,

    Sorry if I understand wrong but I guess you have two option,
    1 You can either have a function in your model like getAPlusB() which will return a+b and you can inject your model in your mediator and then pass result to view or,
    2 You can have a command which will listen value changed event and than calculate a+b and dispatch another event with result and listen to this event from your mediator.

    I hope this helps.
    Bulent

  2. 2 Posted by itsmylifesoham on 14 Feb, 2012 07:25 AM

    itsmylifesoham's Avatar

    thanks for the reply bulupe,

    but what i get from robotlegs documentation , we must not inject model into mediator.
    so i guess i cannot use point 1.

    regarding point 2) i was thinking this can be done, however is it right for a command to listen to events coming from model? i saw the class flow diagram at
    http://www.robotlegs.org/diagram/

    and its clear that their is no communication from model -> command , instead its always from command -> model, the diagram suggests that a model can only dispatch events to the mediator of the view. I have a doubt if your point 2) violates MVC architechture.

  3. 3 Posted by bulupe on 14 Feb, 2012 07:52 AM

    bulupe's Avatar

    Hi again,
    Ok a third way is your model can dispatch an event with a+b as payload and mediator can listen to that event.

    http://knowledge.robotlegs.org/discussions/questions/309-inject-mod... Please read stray's comment.

  4. 4 Posted by itsmylifesoham on 14 Feb, 2012 08:34 AM

    itsmylifesoham's Avatar

    Hi bulupe,

    i think this scheme again violates MVC, because now you model is aware of what is being displayed in your view. tomorrow if i am making another view for displaying a-b, then this scheme would require me to dispatch another event from model with payload as a-b. in MVC the model and view should be decoupled i think.

    according to me the process of doing a+b should go as part of the view logic. "let the view decide how it wants to display the data in the model" . the model only says that it has changed.

    the problem is when handling the "a or b changed event" dispatched from model inside the view i need to manually access the textbox.text property that displays the new a+b value. for eg. if payload of the event are the values a,b then i have to do in the viewlogic textbox.text = a+b; so i am losing the benefits of data binding as i have to do it manually!
    in this case is it a good idea to have a viewmodel acting as a layer between model and view? the viemodel is binded with the view. when a "a or b changed event" comes from the model, we will just alter the viewmodel rather than the view. since the viemodel is binded with view , the view will reflect any change we do to the viewmodel. is this a good scheme for robotlegs?

  5. 5 Posted by bulupe on 14 Feb, 2012 09:15 AM

    bulupe's Avatar

    Hi again,

    Sorry if I cause any misunderstanding, actually imaginary a and b is hard to think of possibilities. But you can have an event for a changed and another for b changed and your mediator can listen to both events. And in your view you can have variables to hold a and b so you can update them with events.
    And you can display both a+b or a-b in your view.
    Moreover as stray mentioned "When a view (or module) lands on stage and it wants a particular type of data the onRegister of the mediator sends a data request event. A command picks this up and gets the data vo from the model and dispatches it on an event. The mediator picks it up and we're away..."

    I hope this helps, by the way I am not a Robotlegs guru :)

  6. Ondina D.F. closed this discussion on 29 Feb, 2012 11:18 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