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

Source for file slow_livesearch.php

Documentation is available at slow_livesearch.php

  1. <?php
  2. /**
  3.  * Using the ordered queue to deal with high latency
  4.  *
  5.  * @category   HTML
  6.  * @package    AJAX
  7.  * @author     Joshua Eichorn <josh@bluga.net>
  8.  * @copyright  2005 Joshua Eichorn
  9.  * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
  10.  * @version    Release: @package_version@
  11.  * @link       http://pear.php.net/package/HTML_AJAX
  12.  */
  13. if (isset($_GET['latency'])) {
  14.     $_SESSION['sleep'$_GET['latency'];
  15. }
  16. ?>
  17. <html>
  18. <head>
  19.  
  20. <script type='text/javascript' src="server.php?client=Util,main,dispatcher,httpclient,request,json,loading,iframe,orderedqueue"></script>
  21. <script type='text/javascript' src="slow_server.php?stub=livesearch"></script>
  22.  
  23. </head>
  24. <body>
  25. <script type="text/javascript">
  26.  
  27. // callback hash, outputs the results of the search method
  28. callback = {
  29.     search: function(result) {  
  30.         var out = "<ul>";
  31.  
  32.         // if we have object this works right, if we have an array we get a problem
  33.         // if we have sparse keys will get an array back otherwise will get an array
  34.         for(var i in result) {
  35.             if (i != '______array') {
  36.                 out += "<li>"+i+" = "+result[i]+"</li>";
  37.             }
  38.         }
  39.         out += "</ul>";
  40.         document.getElementById('target').innerHTML = out;
  41.     }
  42. }
  43.  
  44. // setup our remote object from the generated proxy stub
  45. var remoteLiveSearch = new livesearch(callback);
  46.  
  47. // we could change the queue by overriding the default one, but generally you want to create a new one
  48. // set our remote object to use the ordered queue
  49. remoteLiveSearch.dispatcher.queue = 'ordered';
  50.  
  51. HTML_AJAX.queues['ordered'] = new HTML_AJAX_Queue_Ordered();
  52.  
  53.  
  54. // what to call on onkeyup, you might want some logic here to not search on empty strings or to do something else in those cases
  55. function searchRequest(searchBox) {
  56.     remoteLiveSearch.search(searchBox.value);
  57. }
  58. </script>
  59.  
  60. <p>This is a really basic LiveSearch example, type in the box below to find a fruit</p>
  61. <p>By deafult server add latency to the request, starting with 5 seconds and reducing 1 second per request</p>
  62. <p>An ordered queue has been used which should make results come back in the order they are sent not the order they are received</p>
  63.  
  64. <form action="slow_livesearch.php">
  65. <label>Set current latency too: <input name="latency" size=4><input type="submit">
  66. </form>
  67. <hr><br>
  68.  
  69. Search <input id="search" autocomplete="off" onkeyup="searchRequest(this)">
  70.  
  71. <div style="white-space: pre; padding: 1em; margin: 1em; width: 600px; height: 300px; border: solid 2px black; overflow: auto;" id="target">Target</div>
  72. </body>
  73. </html>
  74. <?php 
  75. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  76. ?>

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