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

Source for file queue_usage.php

Documentation is available at queue_usage.php

  1. <?php
  2. /**
  3.  * Example of Using HTML_AJAX with a queue
  4.  *
  5.  * When using HTML_AJAX your actually always using a queue, its just its a simple one that always performs whatever actions its given immediately
  6.  * HTML_AJAX however provides other queues offering different modes of operation
  7.  *
  8.  * Currently the Queues are (class names are prefixed with: HTML_AJAX_Queue_):
  9.  * Immediate - Default make the request immediately
  10.  * Interval_SingleBuffer - Make request on an interval with holding just the last request between calls
  11.  *
  12.  * Interval_SingleBuffer is generally used for Live Search/Google Suggest type operations
  13.  *
  14.  * @category   HTML
  15.  * @package    AJAX
  16.  * @author     Joshua Eichorn <josh@bluga.net>
  17.  * @copyright  2005 Joshua Eichorn
  18.  * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
  19.  * @version    Release: 0.5.4
  20.  * @link       http://pear.php.net/package/HTML_AJAX
  21.  */
  22. ?>
  23. <html>
  24. <head>
  25.  
  26. <script type='text/javascript' src="server.php?client=Util,main,dispatcher,httpclient,request,json,loading,iframe,queues"></script>
  27. <script type='text/javascript' src="auto_server.php?stub=livesearch"></script>
  28.  
  29. </head>
  30. <body>
  31. <script type="text/javascript">
  32.  
  33. // callback hash, outputs the results of the search method
  34. callback = {
  35.     search: function(result) {  
  36.         var out = "<ul>";
  37.  
  38.         // if we have object this works right, if we have an array we get a problem
  39.         // if we have sparse keys will get an array back otherwise will get an array
  40.         for(var i in result) {
  41.             if (i != '______array') {
  42.                 out += "<li>"+i+" = "+result[i]+"</li>";
  43.             }
  44.         }
  45.         out += "</ul>";
  46.         document.getElementById('target').innerHTML = out;
  47.     }
  48. }
  49.  
  50. // setup our remote object from the generated proxy stub
  51. var remoteLiveSearch = new livesearch(callback);
  52.  
  53. // we could change the queue by overriding the default one, but generally you want to create a new one
  54. // set our remote object to use the rls queue
  55. remoteLiveSearch.dispatcher.queue = 'rls';
  56.  
  57. // create the rls queue, with a 350ms buffer, a larger interval such as 2000 is useful to see what is happening but not so useful in real life
  58. HTML_AJAX.queues['rls'] = new HTML_AJAX_Queue_Interval_SingleBuffer(350);
  59.  
  60.  
  61. // 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
  62. function searchRequest(searchBox) {
  63.     remoteLiveSearch.search(searchBox.value);
  64. }
  65. </script>
  66.  
  67. <p>This is a really basic LiveSearch example, type in the box below to find a fruit</p>
  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 Fri, 04 Apr 2008 18:30:23 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.