tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/471-the-way-value-object-is-displayed-in-a-itemrendererRobotlegs: Discussion 2018-10-18T16:35:36Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/132654102012-01-29T15:26:13Z2012-01-31T18:27:29Zthe way value object is displayed in a itemrenderer<div><p>hi,</p>
<p>I have a sample scenario.<br>
I have a "User" value object as part of my Model. it has just one
property "onlinestatuscode" (can be "available" or
"unavailable").<br>
a service will update this value every 1 minute.</p>
<p>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<br>
"onlinestatus" code property as a selection in a combobox.</p>
<p>what i want to know is how can i achieve two way binding between
the "onlinestatuscode" property on User VO and the<br>
selected value on the combobox? as the onlinestatuscode is a string
and the property to bind on combobox is an int ("selectedindex")
.</p>
<ol>
<li>i can do this by including some view logic in my userlistview
component.</li>
<li>i can make the mediator do convertions between the two types.
eg. when i receive onlinestatuscode changed event mediator gets<br>
it and updates view with the appropriate index. and when combobox
fires changed event to the mediator, it again calls a command<br>
to modify the onlinestatuscode property with a string. but then the
logic goes in the mediator.</li>
<li>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.</li>
</ol>
<p>I am confused, where this logic to convert types should go?<br>
thanks in advance.</p>
<p>to update the</p></div>itsmylifesohamtag:robotlegs.tenderapp.com,2009-10-18:Comment/132654102012-02-02T10:26:43Z2012-02-02T14:02:48Zthe way value object is displayed in a itemrenderer<div><p>Hi,</p>
<p>First of all, sorry for the delayed response!</p>
<p>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.</p>
<p>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.</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/132654102012-02-02T23:00:25Z2012-02-02T23:00:25Zthe way value object is displayed in a itemrenderer<div><p>Hi,</p>
<p>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</p>
<p>Take a look at this simple example - <a href=
"http://pastebin.com/Xv7aWKiz">http://pastebin.com/Xv7aWKiz</a></p></div>Michal Samujlotag:robotlegs.tenderapp.com,2009-10-18:Comment/132654102012-02-03T04:08:11Z2012-02-03T04:08:12Zthe way value object is displayed in a itemrenderer<div><p>First of all thanks for your replies.</p>
<p>@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 : <a href=
"http://code.google.com/p/bindage-tools/">http://code.google.com/p/bindage-tools/</a>
to the tools and search "Two-way binding with validation and
conversion" youll get what i am trying to achieve.</p>
<p>@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".</p>
<p>anyhow for now i guess view method is a gud place to put this
code and its working. thanks again! :)</p></div>itsmylifesohamtag:robotlegs.tenderapp.com,2009-10-18:Comment/132654102012-02-03T09:13:15Z2012-02-03T10:11:49Zthe way value object is displayed in a itemrenderer<div><p>Just to answer your question about where to put conversion
logic:<br>
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)</p>
<p>--EDIT Forgot to say that formatting data for presentational
reasons however is entirely the views responsibility.</p></div>creynderstag:robotlegs.tenderapp.com,2009-10-18:Comment/132654102012-02-03T11:34:54Z2012-02-03T14:00:47Zthe way value object is displayed in a itemrenderer<div><p>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.</p>
<p>I don't think you need BindageTools for this simple scenario,
although this library might help you with memory leaks caused by
mxml bindings.</p></div>Michal Samujlo