AJAX
[ class tree: AJAX ] [ index: AJAX ] [ all elements ]

Source for file proxy_usage_inline_javascript.php

Documentation is available at proxy_usage_inline_javascript.php

  1. <?php
  2. /**
  3.  * Example of Using HTML_AJAX in proxy operation
  4.  *
  5.  * All AJAX calls are handled by this same file and the proxy js is outputed in a js block directly in this script
  6.  * This is a use case very similar to Sajax (Sajax registers functions not objects)
  7.  *
  8.  * The only needed interaction is creation of a new object from the proxy defintion, all AJAX calls happen transparently from
  9.  *
  10.  * If you want to perform async calls a callback object must be passed to the constructor of the object
  11.  *
  12.  * The client JavaScript library is provided by server.php, you could also copy HTML_AJAX.js (all js files combined) or the seperate js files into your webroot
  13.  * from the PEAR data dir and src them directly
  14.  *
  15.  * This example also registers a custom error handler, without this handler errors float out of the app as an Exception
  16.  *
  17.  * An example debugging event listener is also shown
  18.  *
  19.  * @category   HTML
  20.  * @package    AJAX
  21.  * @author     Joshua Eichorn <josh@bluga.net>
  22.  * @copyright  2005 Joshua Eichorn
  23.  * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
  24.  * @version    Release: @package_version@
  25.  * @link       http://pear.php.net/package/HTML_AJAX
  26.  */
  27.  
  28. // include the main HTML_AJAX class
  29. include 'HTML/AJAX.php';
  30.  
  31. // our simple test class
  32. include 'support/test.class.php';
  33.  
  34.  
  35.  
  36. // create an instance of HTML_AJAX
  37. $ajax = new HTML_AJAX();
  38. // register an instance of the test class
  39. $ajax->registerClass(new test());
  40.  
  41. // handle and ajax call, if one is made die, else continue processing
  42. if ($ajax->handleRequest()) {
  43.     die();
  44. }
  45. ?><html>
  46. <head>
  47.  
  48. <!-- the entire client library can be retreived in one call using the all keyword, or each piece can be requested like below -->
  49. <script type='text/javascript' src="server.php?client=Main"></script>
  50. <script type='text/javascript' src="server.php?client=Dispatcher"></script>
  51. <script type='text/javascript' src="server.php?client=HttpClient"></script>
  52. <script type='text/javascript' src="server.php?client=Request"></script>
  53. <script type='text/javascript' src="server.php?client=JSON"></script>
  54. <script type='text/javascript' src="server.php?client=iframe"></script>
  55. <script type='text/javascript' src="server.php?client=loading"></script>
  56.  
  57. <script type='text/javascript'>
  58. <?php
  59.     // generate proxy definition javascript for all registered class
  60.     echo $ajax->generateJavascriptClient();
  61. ?>
  62.  
  63. // definition of the callback javascript class, used to handle async requests
  64. function callback() {}
  65. callback.prototype = {
  66.     echo_string: function(result) {
  67.         document.getElementById('target').innerHTML = result;
  68.     },
  69.     slow_echo_string: function(result) {
  70.         document.getElementById('target').innerHTML = result;
  71.     },
  72.     error_test: function(result) {
  73.         document.getElementById('target').innerHTML = result;
  74.     }
  75. }
  76.  
  77. // function used to clear out the target div
  78. function clearTarget() {
  79.     document.getElementById('target').innerHTML = 'clear';
  80.     document.getElementById('eventLog').innerHTML = 'clear';
  81.     document.getElementById('error').innerHTML = 'clear';
  82. }
  83.  
  84. // register a custom error handler
  85. HTML_AJAX.onError = function(e) {
  86.     msg = "\n\n";
  87.     for(var i in e) {
  88.         msg += i + ':' + e[i] +"\n";
  89.     }
  90.     document.getElementById('error').innerHTML += msg;
  91. }
  92.  
  93. // register custom event handlers, but lets keep the old ones so we still have that automatic loading message
  94. var Open = HTML_AJAX.Open;
  95. var Load = HTML_AJAX.Load;
  96.  
  97. HTML_AJAX.Open = function(request) {
  98.     Open(request);
  99.     document.getElementById('eventLog').innerHTML += "Open: "+request.className+'::'+request.methodName+"\n";
  100. }
  101. HTML_AJAX.Send = function(request) {
  102.     document.getElementById('eventLog').innerHTML += "Send: "+request.className+'::'+request.methodName+"\n";
  103. }
  104. HTML_AJAX.rogress = function(request) {
  105.     document.getElementById('eventLog').innerHTML += "Progress: "+request.className+'::'+request.methodName+"\n";
  106. }
  107. HTML_AJAX.Load = function(request) {
  108.     Load(request);
  109.     document.getElementById('eventLog').innerHTML += "Load: "+request.className+'::'+request.methodName+"\n";
  110. }
  111.  
  112. </script>
  113. </head>
  114. <body>
  115. <script type="text/javascript">
  116. // create a proxy in sync mode
  117. var syncProxy = new test();
  118. // create a proxy in async mode
  119. var asyncProxy = new test(new callback());
  120.  
  121. // run a sync call and set its results to the target div
  122. function syncCall() {
  123.     document.getElementById('target').innerHTML = syncProxy.echo_string("I'm a sync call");
  124. }
  125.  
  126. // run a sync call, callback class will handle its results
  127. function asyncCall() {
  128.     asyncProxy.echo_string("I'm a async call");
  129. }
  130.  
  131. // run a sync call, callback class will handle its results
  132. // the purpose of this slow call is to show off the built in loading messaging
  133. function slowAsyncCall() {
  134.     asyncProxy.slow_echo_string("I'm a slow async call");
  135. }
  136. // same thing, notice how it locks up your browser in Firefox you can't switch to another tab
  137. function slowSyncCall() {
  138.     document.getElementById('target').innerHTML = syncProxy.slow_echo_string("I'm a slow sync call");
  139. }
  140.  
  141. // run a sync call, callback class will handle its results
  142. function serverErrorExample() {
  143.     asyncProxy.error_test("I'm an error generated by trigger error in test.class.php");
  144. }
  145. // same as above, shown for completeness
  146. function serverErrorSyncExample() { 
  147.     document.getElementById('target').innerHTML = syncProxy.error_test("I'm an error generated by trigger error in test.class.php");
  148. }
  149. </script>
  150. <ul>
  151.     <li><a href="javascript:clearTarget()">Clear Target</a></li>
  152.     <li><a href="javascript:syncCall()">Run Sync Echo call</a></li>
  153.     <li><a href="javascript:asyncCall();">Run Async Echo call</a></li>
  154.     <li><a href="javascript:slowAsyncCall();">Run Slow Async Echo call (sleep on server to emulate slow processing)</a></li>
  155.     <li><a href="javascript:slowSyncCall();">Run Slow Sync Echo call (notice how it locks your browser)</a></li>
  156.     <li><a href="javascript:serverErrorExample();">Run a call where an error is generated on the server (Async)</a></li>
  157.     <li><a href="javascript:serverErrorSyncExample();">Run a call where an error is generated on the server (Sync)</a></li>
  158. </ul>
  159. <div style="white-space: pre; padding: 1em; margin: 1em; width: 300px; height: 400px; border: solid 2px black; overflow: auto; float: right;" id="error">Error Target</div>
  160.  
  161. <div style="white-space: pre; padding: 1em; margin: 1em; width: 500px; height: 300px; border: solid 2px black; overflow: auto;" id="target">Target</div>
  162.  
  163. <div style="white-space: pre; padding: 1em; margin: 1em; width: 500px; height: 400px; border: solid 2px black; overflow: auto;" id="eventLog">Event Log
  164. </div>
  165. </body>
  166. </html>

Documentation generated on Sat, 05 May 2007 18:00:20 -0400 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.