• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Any tutorials on using direct actions and/or component actions with AJAX
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Any tutorials on using direct actions and/or component actions with AJAX


  • Subject: Re: Any tutorials on using direct actions and/or component actions with AJAX
  • From: Jake MacMullin <email@hidden>
  • Date: Fri, 4 Jan 2008 16:24:13 +1100

So, if you wish to do WO/Ajax with direct actions, it is quite easy as was previously stated.
If you want to use component actions, it gets more difficult, and requires a little digging into WO internals to understand it all, but it is possible, and we are using it heavily in one of our apps.


What we have done is create our own WODynamicElement subclasses for ajax. We have ajax hyperlinks, forms, data-tables, etc. The key piece of this is that instead of using the component action request handler you use the ajaxRequestHandler

protected void appendAjaxActionURLToResponse(WOResponse iResponse, WOContext iContext, boolean iEscapeHTML) {
String aUrl = iContext .componentActionURL (WOApplication.application().ajaxRequestHandlerKey());
iResponse.appendContentString(aUrl);
}


Hope this helps a bit.

I'm interested in finding out more about this. I've had success using AJAX with Direct Actions, but I am interested in using component actions in some cases. Are there any more complete examples anywhere that demonstrate how to use AJAX with component actions in WebObjects 5.4 without using Project Wonder? I've made a bit of progress using the above code snippet - but I'm still running in to the problem of "backtracking too far". I thought this was the issue that the AJAX Request Handler was meant to address.


In my example I'm attempting to create a simple AJAX-updating counter.

I have the following HTML template in my Main component:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>AJAX Counter</title>
<script src="/HelloComponentAJAX/js/prototype.js" type="text/ javascript"></script>
<script type="text/javascript">
function request(anURL) {
new Ajax.Updater('counter', anURL, { method: 'get' });
}
</script>
</head>
<body>
<div id="counter">
<wo:Counter></wo:Counter>
</div>
</body>
</html>


Here's the contents of my Counter component template:
<wo:SimpleAJAXHyperlink action="[incrementCounter]"></ wo:SimpleAJAXHyperlink>
<wo:WOString value = "[count]"></wo:WOString>


And the Counter.java:
import com.webobjects.appserver.WOComponent;
import com.webobjects.appserver.WOContext;

@SuppressWarnings("serial")
public class Counter extends WOComponent {

   public int count = 0;

   public Counter(WOContext context) {
       super(context);
   }

   public WOComponent incrementCounter() {
   	count++;
   	return(this);
   }

}

And finally, here's my SimpleAJAXHyperlink dynamic element:

import com.webobjects.appserver.WOActionResults;
import com.webobjects.appserver.WOApplication;
import com.webobjects.appserver.WOAssociation;
import com.webobjects.appserver.WOContext;
import com.webobjects.appserver.WODynamicElement;
import com.webobjects.appserver.WOElement;
import com.webobjects.appserver.WORequest;
import com.webobjects.appserver.WOResponse;
import com.webobjects.foundation.NSDictionary;

public class SimpleAJAXHyperlink extends WODynamicElement {

private WOAssociation _action;

public SimpleAJAXHyperlink(String name, NSDictionary<String, WOAssociation> associations, WOElement template) {
super(name, associations, template);
_action = (WOAssociation) associations.objectForKey("action");
}

public void appendToResponse(WOResponse response, WOContext context) {
response.appendContentString("<div onClick=\"request('" + context .componentActionURL (WOApplication.application().ajaxRequestHandlerKey()) + "'); \">click</ div>");
}

public WOActionResults invokeAction(WORequest aRequest, WOContext aContext) {
WOActionResults results = null;

if(aContext.elementID().equals(aContext.senderID())) {
results = (WOActionResults) _action.valueInComponent(aContext.component());
}

return (WOActionResults) (results);
}


}

When I run my application it kind of works (once). If I click on my SimpleAJAXHyperlink it executes the component action, increments the counter and updates the div with the new result. However, when I click a second time I get the "You backtracked too far" page. So - what am I doing wrong?

Regards,

Jake
_______________________________________________
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


References: 
 >Re: Progress bars and HTML generation (From: Lachlan Deck <email@hidden>)

  • Prev by Date: Re: Progress bars and HTML generation
  • Next by Date: Built WOComponents placed in wrong location
  • Previous by thread: Re: Progress bars and HTML generation
  • Next by thread: Simple HelloWorld example app question
  • Index(es):
    • Date
    • Thread