Permalink
Browse files

bringing in new examples from local devel

git-svn-id: http://svn.php.net/repository/pear/packages/HTML_AJAX/trunk@199134 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
Joshua Eichorn
Joshua Eichorn committed Oct 25, 2005
1 parent a4fb374 commit 694bed47bdb9e42b4fd471bcdbadb49531909060
View
@@ -0,0 +1 @@
+See Index.php for details
View
@@ -0,0 +1,65 @@
+<?php
+/**
+ * Advanced usage of HTML_AJAX_Server
+ * Allows for a single server to manage exporting a large number of classes without high overhead per call
+ * Also gives a single place to handle setup tasks especially useful if session setup is required
+ *
+ * The server responds to ajax calls and also serves the js client libraries, so they can be used directly from the PEAR data dir
+ * 304 not modified headers are used when server client libraries so they will be cached on the browser reducing overhead
+ *
+ * @category HTML
+ * @package AJAX
+ * @author Joshua Eichorn <josh@bluga.net>
+ * @copyright 2005 Joshua Eichorn
+ * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/HTML_AJAX
+ */
+
+ // include the server class
+include 'HTML/AJAX/Server.php';
+
+
+// extend HTML_AJAX_Server creating our own custom one with init{ClassName} methods for each class it supports calls on
+class TestServer extends HTML_AJAX_Server {
+ // this flag must be set to on init methods
+ var $initMethods = true;
+
+ // init method for the test class, includes needed files an registers it for ajax
+ function initTest() {
+ include 'test.class.php';
+ $this->registerClass(new test());
+ }
+
+ // init method for the livesearch class, includes needed files an registers it for ajax
+ function initLivesearch() {
+ include 'livesearch.class.php';
+ $this->registerClass(new livesearch());
+ }
+
+
+}
+
+// create an instance of our test server
+$server = new TestServer();
+
+// init methods can also be added to the server by registering init objects, this is useful in cases where you want to dynamically add init methods
+class initObject {
+ // init method for the test class, includes needed files an registers it for ajax
+ function initTest2() {
+ include 'test2.class.php';
+ $this->server->registerClass(new test2());
+ }
+}
+$init = new initObject();
+$init->server =& $server;
+$server->registerInitObject($init);
+
+// you can use HTML_AJAX_Server to deliver your own custom javascript libs, when used with comma seperated client lists you can
+// use just one javascript include for all your library files
+// example url: auto_server.php?client=auto_server.php?client=Util,Main,Request,HttpClient,Dispatcher,Behavior,customLib
+$server->registerJSLibrary('customLib','customLib.js','./');
+
+// handle requests as needed
+$server->handleRequest();
+?>
View
@@ -0,0 +1,5 @@
+// An example custom javascript library served up by the auto_server
+
+function customLibAlert(msg) {
+ alert(msg);
+}
View
@@ -0,0 +1,31 @@
+<?php
+/**
+ * Test class used in other examples
+ * Constructors and private methods marked with _ are never exported in proxies to JavaScript
+ *
+ * @category HTML
+ * @package AJAX
+ * @author David Coallier <davidc@agoraproduction.com>
+ * @copyright 2005 David Coallier
+ * @license LGPL http://www.gnu.org/copyleft/lesser.txt
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/HTML_AJAX
+ */
+require_once 'HTML/AJAX.php';
+
+class error_test
+{
+ function error_test()
+ {
+ $ajax =& new HTML_AJAX;
+ $ajax->debugEnabled = true;
+ $ajax->debugSession = true;
+ set_error_handler(array(&$ajax, '_errorHandler'));
+ trigger_error("I don't know");
+ }
+}
+
+$t =& new error_test;
+print_r($t);
+
+?>
View
@@ -0,0 +1,49 @@
+<?php
+/**
+ * Example of Using HTML_AJAX_Helper
+ *
+ * HTML_AJAX_Helper takes care of basic JavaScript and HTML generation that is needed in many AJAX requests
+ *
+ * @category HTML
+ * @package AJAX
+ * @author Joshua Eichorn <josh@bluga.net>
+ * @copyright 2005 Joshua Eichorn
+ * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/HTML_AJAX
+ */
+
+// include the helper class
+require_once 'HTML/AJAX/Helper.php';
+
+// create an instance and set the server url
+$ajaxHelper = new HTML_AJAX_Helper();
+$ajaxHelper->serverUrl = 'auto_server.php';
+$ajaxHelper->jsLibraries[] = 'customLib';
+?>
+<html>
+<head>
+
+<?php
+ // output a javascript neded to setup HTML_AJAX
+ // by default this is all the libraries shipped with HTML_AJAX, take a look at $ajaxHelper->jsLibraries to edit the list
+ echo $ajaxHelper->setupAJAX();
+?>
+
+</head>
+<body>
+<?php
+ // output a custom loading message
+ echo $ajaxHelper->loadingMessage("Waiting on the Server ...");
+?>
+
+<div id="updateTarget">I'm an update Target</div>
+<?php
+ // update the element using ajax
+ echo $ajaxHelper->updateElement('updateTarget',array('test','echo_string','Some text to echo'),'replace',true);
+?>
+</body>
+</html>
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+?>
View
@@ -0,0 +1,45 @@
+<html>
+<head>
+<title>HTML_AJAX Examples</title>
+</head>
+<body>
+<p>
+These examples cover basic AJAX usage
+</p>
+
+<p>
+All example files are fully commented, reading through them will give you a good overview on how things work.
+</p>
+
+<p>The term proxy in these examples refers to a javascript class that is generated and has functions that map to the eqivalent php class.
+These proxy classes work much in the same way as a SOAP proxy class that is generated from wsdl.
+</p>
+
+<p>
+Front end files for examples, you can actually run these and see some example output
+</p>
+<ul>
+<li><a href='proxyless_usage.php'>proxyless_usage.php</a> - Using HTML_AJAX in standalone mode, possible doesn't require PHP or any backend HTML_AJAX classes</li>
+<li><a href='proxy_usage_inline_javascript.php'>proxy_usage_inline_javascript.php</a> - Single file proxy style usage</li>
+<li><a href='proxy_usage_server.php'>proxy_usage_server.php</a> - Multi-file proxy usage, either server file could be used with this example</li>
+<li><a href='queue_usage.php'>queue_usage.php</a> - An example of using a queue to manage ajax calls, a simple live search example</li>
+<li><a href='helper_usage.php'>helper_usage.php</a> - An example showing the basics of the helper api</li>
+</ul>
+
+<p>
+2 server examples are provided
+</p>
+<ul>
+<li>server.php - Basic server operation, serving ajax calls and client lib requests</li>
+<li>auto_server.php - Advanced server operation, only create php classes as needed</li>
+</ul>
+
+<p>
+Other Example files:
+</p>
+<ul>
+<li><a href='test_speed.php'>test_speed.php</p> - A basic setup for measuring the speed of calls</li>
+</ul>
+
+</body>
+</html>
@@ -0,0 +1,40 @@
+<?php
+/**
+ * Simple test class for doing fake livesearch
+ *
+ * @category HTML
+ * @package AJAX
+ * @author Joshua Eichorn <josh@bluga.net>
+ * @copyright 2005 Joshua Eichorn
+ * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/HTML_AJAX
+ */
+class livesearch {
+ /**
+ * Items to search against
+ */
+ var $livesearch = array(
+ 'Orange',
+ 'Apple',
+ 'Pear',
+ 'Banana',
+ 'Blueberry',
+ );
+
+ /**
+ * Perform a search
+ *
+ * @return array
+ */
+ function search($input) {
+ $ret = array();
+ foreach($this->livesearch as $key => $value) {
+ if (stristr($value,$input)) {
+ $ret[$key] = $value;
+ }
+ }
+ return $ret;
+ }
+}
+?>
@@ -0,0 +1,155 @@
+<?php
+/**
+ * Example of Using HTML_AJAX in proxy operation
+ *
+ * All AJAX calls are handled by this same file and the proxy js is outputed in a js block directly in this script
+ * This is a use case very similar to Sajax (Sajax registers functions not objects)
+ *
+ * The only needed interaction is creation of a new object from the proxy defintion, all AJAX calls happen transparently from
+ *
+ * If you want to perform async calls a callback object must be passed to the constructor of the object
+ *
+ * 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
+ * from the PEAR data dir and src them directly
+ *
+ * This example also registers a custom error handler, without this handler errors float out of the app as an Exception
+ *
+ * An example debugging event listener is also shown
+ *
+ * @category HTML
+ * @package AJAX
+ * @author Joshua Eichorn <josh@bluga.net>
+ * @copyright 2005 Joshua Eichorn
+ * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/HTML_AJAX
+ */
+
+// include the main HTML_AJAX class
+include 'HTML/AJAX.php';
+
+// our simple test class
+include 'test.class.php';
+
+
+
+// create an instance of HTML_AJAX
+$ajax = new HTML_AJAX();
+// register an instance of the test class
+$ajax->registerClass(new test());
+
+// handle and ajax call, if one is made die, else continue processing
+if ($ajax->handleRequest()) {
+ die();
+}
+?><html>
+<head>
+
+<!-- the entire client library can be retreived in one call using the all keyword, or each piece can be requested like below -->
+<script type='text/javascript' src="server.php?client=Main"></script>
+<script type='text/javascript' src="server.php?client=Dispatcher"></script>
+<script type='text/javascript' src="server.php?client=HttpClient"></script>
+<script type='text/javascript' src="server.php?client=Request"></script>
+<script type='text/javascript' src="server.php?client=JSON"></script>
+<script type='text/javascript' src="server.php?client=loading"></script>
+
+<script type='text/javascript'>
+<?php
+ // generate proxy definition javascript for all registered class
+ echo $ajax->generateJavascriptClient();
+?>
+
+// definition of the callback javascript class, used to handle async requests
+function callback() {}
+callback.prototype = {
+ echo_string: function(result) {
+ document.getElementById('target').innerHTML = result;
+ },
+ slow_echo_string: function(result) {
+ document.getElementById('target').innerHTML = result;
+ },
+ error_test: function(result) {
+ document.getElementById('target').innerHTML = result;
+ }
+}
+
+// function used to clear out the target div
+function clearTarget() {
+ document.getElementById('target').innerHTML = 'clear';
+ document.getElementById('eventLog').innerHTML = 'clear';
+ document.getElementById('error').innerHTML = 'clear';
+}
+
+// register a custom error handler
+HTML_AJAX.onError = function(e) {
+ msg = "\n\n";
+ for(var i in e) {
+ msg += i + ':' + e[i] +"\n";
+ }
+ document.getElementById('error').innerHTML += msg;
+}
+
+// register custom event handlers, but lets keep the old ones so we still have that automatic loading message
+var onOpen = HTML_AJAX.onOpen;
+var onLoad = HTML_AJAX.onLoad;
+
+HTML_AJAX.onOpen = function(request) {
+ onOpen();
+ document.getElementById('eventLog').innerHTML += "Open: "+request.className+'::'+request.methodName+"\n";
+}
+HTML_AJAX.onSend = function(request) {
+ document.getElementById('eventLog').innerHTML += "Send: "+request.className+'::'+request.methodName+"\n";
+}
+HTML_AJAX.onProgress = function(request) {
+ document.getElementById('eventLog').innerHTML += "Progress: "+request.className+'::'+request.methodName+"\n";
+}
+HTML_AJAX.onLoad = function(request) {
+ onLoad();
+ document.getElementById('eventLog').innerHTML += "Load: "+request.className+'::'+request.methodName+"\n";
+}
+
+</script>
+</head>
+<body>
+<script type="text/javascript">
+// create a proxy in sync mode
+var syncProxy = new test();
+// create a proxy in async mode
+var asyncProxy = new test(new callback());
+
+// run a sync call and set its results to the target div
+function syncCall() {
+ document.getElementById('target').innerHTML = syncProxy.echo_string("I'm a sync call");
+}
+
+// run a sync call, callback class will handle its results
+function asyncCall() {
+ asyncProxy.echo_string("I'm a async call");
+}
+
+// run a sync call, callback class will handle its results
+// the purpose of this slow call is to show off the built in loading messaging
+function slowAsyncCall() {
+ asyncProxy.slow_echo_string("I'm a slow async call");
+}
+
+// run a sync call, callback class will handle its results
+function serverErrorExample() {
+ asyncProxy.error_test("I'm an error generated by trigger error in test.class.php");
+}
+</script>
+<ul>
+ <li><a href="javascript:clearTarget()">Clear Target</a></li>
+ <li><a href="javascript:syncCall()">Run Sync Echo call</a></li>
+ <li><a href="javascript:asyncCall();">Run Async Echo call</a></li>
+ <li><a href="javascript:slowAsyncCall();">Run Slow Async Echo call (sleep on server to emulate slow processing)</a></li>
+ <li><a href="javascript:serverErrorExample();">Run a call where an error is generated on the server</a></li>
+</ul>
+<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>
+
+<div style="white-space: pre; padding: 1em; margin: 1em; width: 500px; height: 300px; border: solid 2px black; overflow: auto;" id="target">Target</div>
+
+<div style="white-space: pre; padding: 1em; margin: 1em; width: 500px; height: 400px; border: solid 2px black; overflow: auto;" id="eventLog">Event Log
+</div>
+</body>
+</html>
Oops, something went wrong.

0 comments on commit 694bed4

Please sign in to comment.