Topic: Allow Multiple Request URIs

Multiple Request URIs allow to have modules that can work seperately from one another.
by prefixing the Uri,Get,and Post, it is easy to allow users to use multiple Xajax applications on the same page.
I used the sWrapperPrefix so the functions and the uri, get, and post variables are all tied together nicely for tracking.

Here is how I did it.

xajax.inc.php modifications

     * Returns a string containing inline Javascript that sets up the xajax
     * runtime (typically called internally by xajax from get/printJavascript).
     *
     * @param boolean whether this is an additional server or not
     *                  this allows to only return the debug and other variables once.
     * @return string
     */
    function getJavascriptConfig($secondaryserver=false)
    {
        $html  = "\t<script type=\"text/javascript\">\n";
        /* added by charles.a.peterson@ndsu.edu
            prefixing uri, get, and post to allow multiple servers.
            this has been updated in js file and _wrap function also.
        */
        $html .= "var ".$this->sWrapperPrefix."RequestUri=\"".$this->sRequestURI."\";\n";
        $html .= "var ".$this->sWrapperPrefix."DefinedGet=".XAJAX_GET.";\n";
        $html .= "var ".$this->sWrapperPrefix."DefinedPost=".XAJAX_POST.";\n";
        // only do these once, otherwise errors!!
        if(!$secondaryserver){[/cplor]
            $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;
    }

....

    /**
     * Generates the Javascript wrapper for the specified PHP function.
     *
     * @param string the name of the function
     * @param mixed  the request type
     * @access private
     * @return string
     */
    function _wrap($sFunction,$sRequestType=XAJAX_POST)
    {
        /* added sWrapperPrefix by charles.a.peterson@ndsu.edu
            prefixing uri, get, and post 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;
    }

xajax_uncompressed.js modifications.

/* added by charles.a.peterson@ndsu.edu */
var xajaxRequestUri="";
var xajaxDefinedGet=0;
var xajaxDefinedPost=1;

Array.prototype.containsValue = function(valueToCheck)
....
    var loadingTimeout;

    /* added by charles.a.peterson@ndsu.edu */
    this.setConfigFromWrapperVariables = function(sWrapperPrefix)
    {
        // runtime config variables.
        xajaxRequestUri        =eval(sWrapperPrefix+"RequestUri");    
         xajaxDefinedGet        =eval(sWrapperPrefix+"DefinedGet");
        xajaxDefinedPost    =eval(sWrapperPrefix+"DefinedPost");
    }

    // Sends a XMLHttpRequest to call the specified PHP function on the server
    // * sRequestType is optional -- defaults to POST
    /* added sWrapperPrefix, charles.a.peterson@ndsu.edu */
    this.call = function(sFunction, aArgs, sRequestType, sWrapperPrefix)
    {
        var i,r,postData;
        
        /* added setConfigFromWrapperVariables call, charles.a.peterson@ndsu.edu */
        this.setConfigFromWrapperVariables(sWrapperPrefix);


....

hopefully this will see its way into the next version
there may be a better, more efficient, way to do this...but it works for me!!

Re: Allow Multiple Request URIs

Looks interesting! smile

xajaxDefinedGet, xajaxDefinedPost will never change.  They're just the values of the static constant definitions XAJAX_GET and XAJAX_POST, so you can cut back a little of your code by removing the double definitions.

Also, can you keep your posting to just one forum.

Re: Allow Multiple Request URIs

Thanks!!!


here is the update...

only two line changes needed in xajax_uncompressed.js / xajax.js from original files.

xajax_uncompressed.js modifications.

    // add the sWrapperPrefix as an argument to the call function
    /* added sWrapperPrefix, charles.a.peterson @ ndsu.edu */
    this.call = function(sFunction, aArgs, sRequestType, sWrapperPrefix)

        // replace this line
        //var uri = xajaxRequestUri;
        // with this one, it is the only time xajaxRequestUri is used in the javascript.
        var uri = eval(sWrapperPrefix+"RequestUri");

very minimal changes here also.

xajax.inc.php modifications

    /**
     * Returns a string containing inline Javascript that sets up the xajax
     * runtime (typically called internally by xajax from get/printJavascript).
     *
     * @param boolean whether this is an additional requestURI or not
     *                  this allows to only return the debug and other variables once.
     * @return string
     */
    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;
    }

....

    /**
     * Generates the Javascript wrapper for the specified PHP function.
     *
     * @param string the name of the function
     * @param mixed  the request type
     * @access private
     * @return string
     */
    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;
    }

I really think this is worth the couple lines of extra code.
being able to call xajax based requestURIs allows running two different xajax apps on the same page.
And all you need to do is change the sWrapperPrefix so it is unique to each app.

Re: Allow Multiple Request URIs

Is there any chance that this feature will be implemented in some way in a future release (perhaps as an optional parameter for xajax::registerFunction / xajax.call)? It would allow to run multiple xajax applications/modules with different Request URIs using a single xajax instance which could be integrated into a project's API. (I'm doing this right now using artistan's hack and one instance per module.)

Although, if you do it this way, all modules would have to share the same remaining global settings what might not be desirable. (Besides other implications that I am not aware of.) Nevertheless, I think it's incredibly useful.

Keep up the great work, everyone!

-marc

Re: Allow Multiple Request URIs

Im interested in the same thing, I need to be able to call xajax across multiple servers within the same domain on the same page

Re: Allow Multiple Request URIs

Ok, but you not wrote about processRequests();.

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

    $xajaxSecond = new xajax('myotherfile.php', 'second_');
    $xajaxSecond->registerFunction('functionB');
   
    $xajaxFirst ->processRequests(); // works
    $xajaxSecond->processRequests(); // functionB not works
?>

Where is solution?

Re: Allow Multiple Request URIs

hava8222, you cant call two xajax object like that.  Only the first processRequests will be run, as you've found.  I believe that in xajax 0.5 you can choose to point your functions in javascript to a different page on your server.

8 (edited by openagora 2007-04-10 9:21:49 PM)

Re: Allow Multiple Request URIs

in few steps  ...

is not necesary modify our use of XAJAX... these changes are for internal work of the class
(i need this to use more than one new xajax(url) with diferent url in the same page.)


FIRST STEP

IN xajax.inc.php add the following CODE

in class definition

class xajax
{
    var $uriOA;  //add this class variable
...

in constructor

....
function xajax($sRequestURI="",$sWrapperPrefix="xajax_",$sEncoding=XAJAX_DEFAULT_CHAR_ENCODING,$bDebug=false)
    {
    $uriOA=substr(md5(microtime()),rand(0,20),6);  //create a random string (perhaps there are better ways)
    $this->uriOA="oa".$uriOA;  // append at begining a string like "oa" , this will be used as name of a JS variable, then cant start with a number!

in getJavascriptConfig function

....
function getJavascriptConfig()
    {
        $html  = "\t<script type=\"text/javascript\">\n";
        //$html .= "var xajaxRequestUri=\"".$this->sRequestURI."\";\n";  //OLD WAY
                $html .= "var {$this->uriOA}xajaxRequestUri=\"".$this->sRequestURI."\";\n"; //NEW WAY
....

in  getJavascriptInclude function

    //$html = "\t<script type=\"text/javascript\" src=\"" . $sJsURI . $sJsFile . "\"></script>\n";  //coment this line!!!...  and add manually only once xajax.js

now... in xajax.js

....    
        // * sRequestType is optional -- defaults to POST
    this.call = function(sFunction, aArgs, sRequestType,uriOA)  //add an extra argument to the function
    {
    
 ...

few lines more above

 
      //var uri = xajaxRequestUri;  //OLD WAY
        var uri = eval(uriOA+'xajaxRequestUri'); //NEW WAY!

tested on iexplore6 7 and Mozilla... works

<?php

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

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

    $xajaxOtra = new xajax('myfileotherotherfile.php');
    $xajaxOtra->registerFunction('functionC');
   

?>


regards!!!...

(sorry for my english!)


Bernardo Jerez
www.openagora.com