Re: Event handling question
Re: Event handling question
- Subject: Re: Event handling question
- From: Art Isbell <email@hidden>
- Date: Fri, 4 Apr 2003 15:14:29 -1000
On Thursday, April 3, 2003, at 12:48 PM, Richard L. Peskin wrote:
The application consists of two
WOConditionals, one wrapping a simple Form with two user input text
fields and the other wrapping two text fields. My question concerns the
adduser() method which is triggered when the Submit button is pressed.
If the "entryIncomplete" is true (user didn't fill in text fields) the
console message from addUser() is printed. But if "entryIncomplete" is
false (triggering the else branch in entry(incomplete()), the addUser()
message is not printed and the message in that else branch is printed
twice????
public WOComponent addUser()
{
System.out.println("Submit Button clicked" );
System.out.println();
return null;
}
public boolean entryIncomplete()
{
boolean entryIncomplete;
if (personName == null || favoriteFood == null ||
personName.equals("") || favoriteFood.equals("") ){
System.out.println("The entry is incomplete.");
entryIncomplete = true;
}
else {
System.out.println("The entry is complete.");
entryIncomplete = false;
}
return entryIncomplete;
}
It's sometimes difficult to understand when and why various component
Java methods are invoked. I find temporarily overriding the three
methods involved with request and response processing,
takeValuesFromRequest(), invokeAction(), and appendToResponse(), to be
helpful in understanding. If you override these methods in the Main
component such that they print a message and add logging messages to
the component instance variable "getter" methods, you'll see the
following in the console. I don't fully understand everything that's
happening, so those who do, please chime in.
Launch the app:
Application's awake invoked.
Session's awake invoked.
Main's awake invoked.
appendToResponse()
The entry is incomplete.
Getting personName: null
Getting favoriteFood: null
The entry is incomplete.
The initial request doesn't involve a component in your app, so
takeValuesFromRequest() and invokeAction() aren't invoked. But the
response component is Main. As the Main component is parsed from top
to bottom, left to right, to produce a response page,
entryIncomplete(), bound to two WOConditionals, is invoked the first
time. personName and favoriteFood are null, so the incomplete entry
message is printed. The component is then asked for the values of the
personName and favoriteFood component instance variables to set the
values of the HTML input elements. Both are null. Then the second
WOConditional is evaluated resulting in the second incomplete entry
message.
Click the submit button with empty text fields:
Application's awake invoked.
Session's awake invoked.
Main's awake invoked.
takeValuesFromRequest()
The entry is incomplete.
Getting personName: null
Getting favoriteFood: null
The entry is incomplete.
The request is received. The personName and favoriteFood form values
are null, so when the request component, Main, is parsed, the
incomplete entry message is printed. Then the personName and
favoriteFood values in the Main object are accessed (not sure why).
Then the second WOConditional is evaluated and an incomplete entry
message is printed.
invokeAction()
The entry is incomplete.
Submit clicked.
Because an action is associated with the submit button, invokeAction()
is invoked. I think only one WOConditional is evaluated during
invokeAction() because parsing stops when the submit button associated
with the action is parsed. Someone please correct me if I'm wrong.
appendToResponse()
The entry is incomplete.
Getting personName: null
Getting favoriteFood: null
The entry is incomplete.
Because the action method returns null, Main is the response
component. Because no values for personName and favoriteFood were
entered, processing similar to that in the request is repeated during
the response.
Click the submit button with non-empty text fields:
Application's awake invoked.
Session's awake invoked.
Main's awake invoked.
takeValuesFromRequest()
The entry is incomplete.
Getting personName: null
Setting personName to Art Isbell
Getting favoriteFood: null
Setting favoriteFood to Thai curry
The entry is complete.
The request is received. Prior to the personName and favoriteFood
values being accessed, the first WOConditional evaluates to true
resulting in the incomplete entry message. Then the existing
personName component value is accessed (still don't know why :-)
Because the personName form value is non-null, its value is set in the
Main component object. This is repeated for favoriteFood. Thus when
the second WOConditional is evaluated, it prints the complete entry
message.
invokeAction()
The entry is complete.
The entry is complete.
Because an action is associated with the submit button, invokeAction()
is invoked. The first WOConditional evaluates to false, so the submit
button won't be included in the response page (is this why the submit
action is not invoked?). Then the second WOConditional is evaluated.
appendToResponse()
The entry is complete.
The entry is complete.
Getting personName: Art Isbell
Getting favoriteFood: Thai curry
If no action is invoked, I suppose the response component will be the
same as the request component (is this true?), so Main is the response
component. Both WOConditionals are evaluated resulting in complete
entry messages being printed. Because the second WOConditional
evaluates to true, personName and favoriteFood values are accessed as
the WOString elements are parsed.
This graphically demonstrates why methods bound to dynamic elements
should not implement a lot of processing; these methods are invoked
repeatedly during the request-response cycle. It may sometimes be
better to bind dynamic elements to component instance variables rather
than to methods or to cache the return values of bound methods in
component instance variables. Of course, each instance variable adds
to the memory footprint of a WO app, so the classic time vs. space
tradeoffs remain.
Aloha,
Art
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.