Hi Jerry and list,
I have just implemented the code you provided below in a checkbox array and it has really sped up the response in my application compared to my previous implementation. Thanks!
The next step for me is to make the selectionList persistent in the database so that when the user comes back to the list they can see what elements have already been checked. I have a solution, but I am not sure if it is the most efficient possible. Would you all mind telling me if there is a more accepted way of doing this?
Thanks,
David
----
NSMutableArray mostRecentRemoval = new NSMutableArray();
NSMutableArray mostRecentAddition = new NSMutableArray();
/** @TypeInfo Element */
public NSArray favourites() {
if (favourites == null) {
favourites = user.favouriteElements();
}
return favourites;
}
/** @TypeInfo Element */
public NSMutableArray selectionList() {
if (selectionList == null) {
selectionList = new NSMutableArray(favourites.mutableClone());
}
return selectionList;
}
public boolean elementChecked()
{
return selectionList.containsObject(anElement);
}
public void setElementChecked(boolean newElementChecked) {
// Jerry Walker's code:
// is the Artifact already on the selection list?
boolean oldValue = elementChecked();
// if selected, and not already on the list, add it
if (newElementChecked && !oldValue) {
selectionList.addObject(anElement);
mostRecentAddition.addObject(anElement);
}
// if not selected but already on the list, remove it
else if ( !newArtifactChecked && oldValue) {
selectionList.removeObject(anElement);
mostRecentRemoval.addObject(anElement);
}
// if selected and already on the list, do nothing
// if not selected and not on the list, do nothing
}
public WOComponent submit()
{
Session session = (Session)session();
EOEditingContext ec = session.defaultEditingContext();
int y = 0;
while (mostRecentRemoval.count() != y) {
anElement = (Element)mostRecentRemoval.objectAtIndex(y);
user.removeObjectFromBothSidesOfRelationshipWithKey(anElement, "favouriteElements");
++y;
}
int z = 0;
// while (selectionList.count() != z) {
while (mostRecentAddition.count() != z) {
anElement = (Element)mostRecentAddition.objectAtIndex(z);
user.addObjectToBothSidesOfRelationshipWithKey(anElement, "favouriteElements");
++z;
}
ec.saveChanges();
return null;
}
On 24 Oct 2006, at 3:46 PM, Jerry W. Walker wrote:
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:
That's it, simple, fast and elegant.
Regards,
Jerry