VO in DataGrid - are getters for values in VO a good practice?

andreas's Avatar

andreas

08 Nov, 2011 08:45 PM

Hi,

I have a list of vos in an ArrayCollection that were shown in a DataGrid.
Each VO has a type-field ("READ" / "NEW_MESSAGE"), I use dataField="type" to show this.
Instead of this type-field I want to show a different text "read" and "unread".
I could write a getter that returns the right text:

public function get messageType():void {
    switch(type) {
        case 'READ':
            return 'read'
        case 'NEW_MESSAGE':
            return 'unread'
    }
}

and use this as the dataField.
But is it a good practice to write those getters for VO-Objects or is there another way to handle those modifications of model data for the view?

  1. 1 Posted by andreas on 08 Nov, 2011 08:50 PM

    andreas's Avatar

    I could of course use an ItemRenderer but I think that's a bit to heavy for this simple task

  2. Support Staff 2 Posted by Ondina D.F. on 09 Nov, 2011 09:24 AM

    Ondina D.F.'s Avatar

    Hi Andreas,

    I’m not quite sure I understand your use case.

    Take a look at Stray’s point of view regarding VO’s and getters:

    http://knowledge.robotlegs.org/discussions/questions/352-stray-clar...

    If this doesn’t answer your question, please come back with more details :)

    Ondina

  3. 3 Posted by andreas on 09 Nov, 2011 09:59 AM

    andreas's Avatar

    Hi Ondina:
    Strays VO definition:
    A VO is an immutable representation of complex state. A snapshot of how things are at a moment in time. [...] So - my VOs have getters. That's all.

    That's how I have VOs in mind, so my question is, is it a good practice to have functions in the VO that format or translate text for the view or should I keep the VO very clean and do this formatting-stuff somewhere else?

  4. 4 Posted by andreas on 09 Nov, 2011 10:10 AM

    andreas's Avatar

    P.S. reading it a second time I think I havn't been very clear in the title and in the first post, sorry about that (of course, getters in the vo are good practice, my real question is "where do I format the data from the vo when the list ov VOs from the model are passed directly to the view"?

    "modifications of model data for the view" - that was very bad verbalized

  5. Support Staff 5 Posted by Ondina D.F. on 09 Nov, 2011 10:14 AM

    Ondina D.F.'s Avatar

    Simply put, I would use a Model to manipulate the VO’s data, and then dispatch an event with the VO as a payload. The Mediator would listen for this event, grab the payload and pass it to the View.

  6. Support Staff 6 Posted by Ondina D.F. on 09 Nov, 2011 10:23 AM

    Ondina D.F.'s Avatar

    “where do I format the data from the vo when the list ov VOs from the model are passed directly to the view”
    What do you mean?
    Are you asking if the View (UI Component) should do this or if you should do it in the Mediator?
    Answer: in the View (Component).
    What kind of formatting are you talking about?

  7. Support Staff 7 Posted by Ondina D.F. on 09 Nov, 2011 10:31 AM

    Ondina D.F.'s Avatar

    I’ve re-read your original question. You are referring to ItemRenderers, right?
    Have you read the discussions about Item Renderers already?

  8. 8 Posted by andreas on 09 Nov, 2011 10:51 AM

    andreas's Avatar

    With formating I mean for example show "unread" instead of "NEW_MESSAGE" or show percent values as XX % instead of a floating value.

    Are you asking if the View (UI Component) should do this or if you should do it in the Mediator?

    Answer: in the View (Component).

    OK, but that brings me to the problem: How and where in the view do I do this? I could write an ItemRenderer for my view component but I think that's a bit to heavy (especially for the performance, I want to minimize the amount of ItemRenderers in my Application), besides these components support the dataField-Keyword, but if I want to use that I need to have the formated data aveilable from the object that is passed to the view (which is my VO), so the easiest way would be to do this formating in the VO.
    But that is in conflict with Strays VO definition.

    I have searched the discussions about ItemRenderers, but I did not find anything about this problem, do you mean a special ItemRenderer discussion?

  9. 9 Posted by Stray on 09 Nov, 2011 11:08 AM

    Stray's Avatar

    For really simple rendering of values (eg, a 'Label' field that returns the 'Title' property of a VO in Proper case) I think it's fine to put that kind of formatting in the VO.

    As long as it's just a getter of course.

    If it's any more complex than that then usually I'll do that in the Model and pass the already-processed value to the VO on construction.

    The question is really whether the 'logic' is specific to the data or specific to the view. If it's specific to the view then I'll use a label function in the view to process the VO.

    I only use AS3, not Flex, so perhaps there isn't an equivalent of the label function, other than itemRenderers?

    hth,

    Stray

  10. Support Staff 10 Posted by Ondina D.F. on 09 Nov, 2011 11:26 AM

    Ondina D.F.'s Avatar

    Yep, Flex DataGridColumn has a labelFunction.

  11. 11 Posted by andreas on 09 Nov, 2011 11:46 AM

    andreas's Avatar

    Thank you very much! labelFunction - that was what I was looking for!

    Its kind of odd that I have not seen labelFunction before, if I had just taken a deeper look into the flex component reference I would have saved us all the time for this discussion, so sorry again!

  12. andreas closed this discussion on 09 Nov, 2011 11:47 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