Re: WORepetition, checked attribute question
Re: WORepetition, checked attribute question
- Subject: Re: WORepetition, checked attribute question
- From: "Jerry W. Walker" <email@hidden>
- Date: Tue, 24 Oct 2006 18:46:30 -0400
Hi, Chip (and Robert),
I think Mark Morris provided the answer to your question why your
SetSelectedItem method is being called repeatedly.
If your goal is to have a list of items (such as the items of a
dinner menu) produced by a WORepetition with a check box next to each
to indicate whether that item is included in, or excluded from, some
list of interest to you (such as an order from the menu), then there
is a WO idiom to handle this.
Although Robert's method will work (it's advice from a Walker, it's
almost certain to work!), it has the unfortunate side effect of
muddying the line between the Model and the View in an MVC
architecture (on which WO is built).
Here's the explanation and the code for the idiom which cleverly
avoids this side effect:
Presume that you have a list of Items, itemList, over which you want
to iterate and associate a checkbox. Presume also that you want to
capture the results, selectionList, of the user's checking some set
of checkboxes when they submit the form:
=======================================
public NSArray itemList;
public Item currentItem;
public NSMutableArray selectionList;
// Presume that itemList and selectionLists have been initialized.
public boolean itemSelected()
{
return selectionList.containsObject(currentItem);
}
public void setItemSelected(boolean value)
{
// is the Item already on the selection list?
boolean oldValue = itemSelected();
// if selected, and not already on the list, add it
if (value && !oldValue)
selectionList.addObject(currentItem);
// if not selected but already on list, remove it
else if (!value && oldValue)
selectionList.removeObject(currentItem);
// if selected and already on list, do nothing
// if not selected, but not on list, do nothing
}
=======================================
For the WORepetition, bind the currentItem to the "item" binding,
bind the itemList to the "list" binding.
For the iterated WOCheckBox, bind itemSelected to the "checked"
binding. This, of course, binds to the two accessor methods for which
there is no instance variable. The get accessor simply checks to see
whether the item is in the selected list. The set accessor deals
with the four cases:
is checked on selection list action
true true do nothing
true false add to list
false true remove from list
false false do nothing
That's it, simple, fast and elegant.
Regards,
Jerry
On Oct 24, 2006, at 5:03 PM, Robert Walker wrote:
Chip,
If what you are trying to accomplish is what I think, there may be
a much better solution to your problem than using an onclick
javascript on the checkbox.
I am assuming your are using your WORepetition to iterate over EO's
(or other KVC compliant class).
When I have this situation, I simple add a transient boolean
instance variable to my EO (KVC compliant) class. Then I bind the
WOCheckbox to the transient boolean. Now I can determine which
objects are selected by reading the instance variable on the object
used as an iterator in the repetition. Using this approach there
is no need to keep a separate array of "selected" objects since the
objects themselves track this state. It has worked very well for me.
Understand that I'm making many assumptions about your design goal
so this may not work in your case.
On Oct 24, 2006, at 4:44 PM, Chip Myers wrote:
Hi, I'm having trouble combining a WOCheckbox and WORepetition. I
have a method named SetSelectedItem(), which adds or removes an
element of the WORepetition to an NSMutableArray. I've added an
onClick attribute to my WOCheckbox, which calls SetSelectedItem()
My problem occurs here: whenever the page first loads, the
SetSelectedItem method is called at each iteration of the
WORepetition, before I even begin to move my mouse near it to
click/unclick it. Is there anyway to avoid this??
Thanks,
Mike
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden
This email sent to email@hidden
--
Robert Walker
email@hidden
--
__ Jerry W. Walker,
WebObjects Developer/Instructor for High Performance Industrial
Strength Internet Enabled Systems
email@hidden
203 278-4085 office
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden