Oh sorry.  Yes I did intend to send that to just the list.  Guess I yanked out the wrong address.  :)

On Oct 24, 2006, at 8:23 PM, Jerry W. Walker wrote:

Yo, cousin Robert,

Did you mean to send this to the list, or just to me?

On Oct 24, 2006, at 8:04 PM, Robert Walker wrote:

Yea go with Jerry's design pattern, it's better.  And I'll do the same from now on.  I just hadn't thought of doing it this way.

In all fairness, I hadn't thought of doing it this way either. It's the method that was taught in the Apple WO classes I used to teach. I thought it was incredibly clever once I thought it through. That didn't occur until I had this very design requirement soon after taking the Apple class and was trying to figure out how to get it to work without violating MVC. It was only then that I went back to the class notes and looked a bit deeper at this little gem.

Regards,
Jerry

On Oct 24, 2006, at 6:46 PM, Jerry W. Walker wrote:

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 protected])
Help/Unsubscribe/Update your Subscription:

This email sent to [EMAIL PROTECTED]

--
Robert Walker

--
__ Jerry W. Walker,
   WebObjects Developer/Instructor for High Performance Industrial Strength Internet Enabled Systems

    203 278-4085        office




--
Robert Walker

There are 10 types of people in the world, those who count in binary, and those who don't.




--
__ Jerry W. Walker,
   WebObjects Developer/Instructor for High Performance Industrial Strength Internet Enabled Systems

    203 278-4085        office




--
Robert Walker



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to