Xajax 0.2: Tips and Tricks: Using Multiple Request URIs






Xajax 0.2: Tips and Tricks: Using Multiple Request URIs

NOTE: This document describes a hack. You will need to directly edit xajax core files.

The technique has been tested with xajax version 0.2.4.

Contents

[edit] How does it work?

artistan in the forums came up with a method to allow running multiple xajax instances with different request URIs on one page. This is achieved through automatically prefixing every instance's xajaxRequestUri JavaScript variable with the sWrapperPrefix parameter you can specify when initializing the xajax object in PHP. This way, you might do something like this:

<?php
    $xajaxFirst = new xajax('myfile.php''first_');
    $xajaxFirst->registerFunction('functionA');

    $xajaxSecond = new xajax('myotherfile.php''second_');
    $xajaxSecond->registerFunction('functionB');
?>

<head>
...
<?php
    $xajaxFirst->printJavaScript();
    $xajaxSecond->printJavaScript();
?>
...
</head>

All xajax calls prefixed with "first_" would be sent to myfile.php while all calls prefixed with "second_" would be handled by myotherfile.php.

NOTE: The printJavaScript() functions will add generic output multiple times to the page. It seems that you have to live with it.

[edit] What do I have to change?

[edit] xajax_js (or xajax_uncompressed.js)

  1. Add the sWrapperPrefix as an argument to the call function:

    this.call = function(sFunction, aArgs, sRequestType, sWrapperPrefix)

  2. Replace the line

    var uri = xajaxRequestUri;

    with this one:

    var uri = eval(sWrapperPrefix+"RequestUri");

[edit] xajax.inc.php

  1. Replace the method getJavascriptConfig with this version:

    function getJavascriptConfig($additionalRequestURI=false)
    {
        $html  "\t<script type=\"text/javascript\">\n";
        /*     added by charles.a.peterson @ ndsu.edu
              prefixing requesturi to allow multiple servers.
                 this has been updated in js file and _wrap function also.
        */
        $html .= "var ".$this->sWrapperPrefix."RequestUri=\"".$this->sRequestURI."\";\n";
        // only do these once, otherwise errors!!
        if(!$additionalRequestURI){
            $html .= "var xajaxDefinedGet=".XAJAX_GET.";\n";
            $html .= "var xajaxDefinedPost=".XAJAX_POST.";\n";
            $html .= "var xajaxDebug=".($this->bDebug?"true":"false").";\n";
            $html .= "var xajaxStatusMessages=".($this->bStatusMessages?"true":"false").";\n";
            $html .= "var xajaxWaitCursor=".($this->bWaitCursor?"true":"false").";\n";
            $html .= "var xajaxLoaded=false;\n";
        }
        foreach($this->aFunctions as $sFunction => $bExists) {
            $html .= $this->_wrap($sFunction,$this->aFunctionRequestTypes[$sFunction]);
        }
    
        $html .= "\t</script>\n";
        return $html;
    }
    
  2. Replace the method _wrap with this code:

    function _wrap($sFunction,$sRequestType=XAJAX_POST)
    {
        /* added sWrapperPrefix by charles.a.peterson @ ndsu.edu
            prefixing requesturi to allow multiple servers.
            this has been updated in js file and getJavascriptConfig function also.
        */
        $js "function ".$this->sWrapperPrefix."$sFunction(){return xajax.call(\"$sFunction\", arguments, "
                .$sRequestType.",\"".$this->sWrapperPrefix."\");}\n";
        return $js;
    }
    

[edit] Links