the way value object is displayed in a itemrenderer
hi,
I have a sample scenario.
I have a "User" value object as part of my Model. it has just one
property "onlinestatuscode" (can be "available" or
"unavailable").
a service will update this value every 1 minute.
my model is basically a arraycollection of "User" VO's. the item
renderer to render a User VO in the online users list displays
the
"onlinestatus" code property as a selection in a combobox.
what i want to know is how can i achieve two way binding between
the "onlinestatuscode" property on User VO and the
selected value on the combobox? as the onlinestatuscode is a string
and the property to bind on combobox is an int ("selectedindex")
.
- i can do this by including some view logic in my userlistview component.
- i can make the mediator do convertions between the two types.
eg. when i receive onlinestatuscode changed event mediator gets
it and updates view with the appropriate index. and when combobox fires changed event to the mediator, it again calls a command
to modify the onlinestatuscode property with a string. but then the logic goes in the mediator. - make the onlinestatuscode in the "User" VO an int. when the service returns the string value for onlinestatuscode, use a factory method to convert this string to int and set in the VO. (in this case is can use the twoway binding in flex @{onlinestatuscode} in my item renderer.
I am confused, where this logic to convert types should go?
thanks in advance.
to update the
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
Support Staff 1 Posted by Ondina D.F. on 02 Feb, 2012 10:26 AM
Hi,
First of all, sorry for the delayed response!
Even though the scenario you’ve described sounds pretty simple, I’m afraid I don’t understand it completely :) Therefore I’m not sure were I’d do the type conversion: in the Model or in the View. My first impulse was to let the View convert the type by binding the selectedIndex to a function, which would decide how to interpret the VO’s value. But maybe something along the lines of your 3rd option would be better.
Anyway, you’re right, you shouldn’t let the Mediator do the conversion. You can call a view’s method in your mediator with the event’s payload as a parameter and let the view do the conversion.
2 Posted by Michal Samujlo on 02 Feb, 2012 11:00 PM
Hi,
Maybe you should bind UserVO.onlinestatus to dropdown's selectedItem instead of selectedIndex? Use list of all online statuses as dropdown dataProvider. selectedItem will change when UserVO.onlinestatus changes and you don't need any type conversion
Take a look at this simple example - http://pastebin.com/Xv7aWKiz
3 Posted by itsmylifesoham on 03 Feb, 2012 04:08 AM
First of all thanks for your replies.
@Ondina D.F : I am doing this for the timebeing, that is calling a view method from mediator. What i was trying to achieve is two way binding with conversion. In microsoft silverlight they too have two way binding as in flex, but they have a concept of converters for two way binding. For flex i could find "bindage tools" library to achieve two way binding with conversion. may be if you see this link : http://code.google.com/p/bindage-tools/ to the tools and search "Two-way binding with validation and conversion" youll get what i am trying to achieve.
@Michal : thanks for the quick code sample! but it wont make a difference right? becoz lets say the online status is "UNAVAILABLE" in the model . but in view i want it to appear as "Inactive". And lets say when i drop the combobox and select "Active" i want this action to set the value of onlinestatus property in our underlying model to be "AVAILABLE".
anyhow for now i guess view method is a gud place to put this code and its working. thanks again! :)
Support Staff 4 Posted by creynders on 03 Feb, 2012 09:13 AM
Just to answer your question about where to put conversion logic:
Views, models and services should be entirely independent from each other, this means that all data that passes between those three can be completely differently structured. So type conversions should happen as an extra step during the passing of data from service to model and from model to view. Personally I use parser classes in my services that pass on correctly structured data to a command which in turn puts the data in the correct model(s). When pulling data from a model to a view, either I let the mediator handle the type conversion, again through some kind of helper class, or I let a command pull the data from the model, run it through a converter and return it to the requesting mediator (when using a request-response mechanism)
--EDIT Forgot to say that formatting data for presentational reasons however is entirely the views responsibility.
5 Posted by Michal Samujlo on 03 Feb, 2012 11:34 AM
As creynders noticed, it is views responsibility to convert data for presentation. In my example you can write your own ItemRenderer that will format and display online status any way you like. Still, binding between combobox.selectedItem and UserVO.onlinestatus will work as expected.
I don't think you need BindageTools for this simple scenario, although this library might help you with memory leaks caused by mxml bindings.
itsmylifesoham closed this discussion on 13 Feb, 2012 06:43 AM.