Topic: assigning values to a callback function

Hi all,

I think a nice feature would be, that you can set up a callbackfunction on clientside and assign values to the function call on serverside...

what I imagine:


JS call that expects a callback function

xajax.call('myFunction', { myEventParameter: 'myData', parameters: [ 'test1', 'test2' ],callback:this.updateSomething } );

var myClass = function() {
  updateSomething: function(value1,value2,value3) {

  } 
}

On PHP side you can set the parameters that xajax will use to make the callback...

function myFunction($aFormValues) {

$objResponse = new xajaxResponse();    

$objResponse->callbackValues(array("value1","value2","value3"));

return $objResponse;

}

What I expect:

make a ansynchronous request that calls a function onComplete without loosing the scope.


What do you think about it? or do you have any questions?

Re: assigning values to a callback function

q_no wrote:

make a ansynchronous request that calls a function onComplete without loosing the scope.

Kind of like combining the best parts of synchronous and asynchronous xajax calls together?
I like it smile  Want to have a crack at writing the code for it?

Re: assigning values to a callback function

BigBrownChunx wrote:

Kind of like combining the best parts of synchronous and asynchronous xajax calls together?
I like it smile  Want to have a crack at writing the code for it?

crack? drugs are bad hm'kay smile

I'll try to modify my xajax install... maybe I 'm lucky, maybe not smile we'll see...

4 (edited by q_no 2007-04-26 4:35:03 PM)

Re: assigning values to a callback function

ok, I got  it.... first try with only one parameter for the callbackfunction

xajaxResponse.inc.php wrote:

function callbackValue($aAttributes) {
        $this->addCommand(array('n'=>'cbv'),$aAttributes);
        return $this;
    }

xajax_core.js wrote:

xajax.commands['cbv'] = function(args) {
    if (undefined !== args.request.callbackfunc)
        args.request.callbackfunc(args.data);
    return;
}

xajax.parseAttributes = function(child, obj) {
    var iLen = child.attributes.length;
    for (var i = 0; i < iLen; ++i) {
        var attr = child.attributes[i];
        switch (attr.name) {
        case "n":
            obj.cmd = attr.value;
            break;
        case "t":
            obj.id = attr.value;
            break;
        case "p":
            obj.property = attr.value;
            break;
        case "c":
            obj.type = attr.value;
            break;
        case "f":
            obj.func = attr.value;
            break;
        case "cbv":
            obj.cmd = attr.value;
            break;
        }
    }
}

So, now I can do something like this

JavaScript

myclass = function() {
    testfunc: function(bla) {            
    xajax.call('testfunc', { parameters:[{}], callbackfunc: this.testfunc2.createDelegate(this)});         
        
    },
    testfunc2: function(bla) {
        alert(bla);        
    }
}

PHP

function testfunc($aFormValues) {
    $objResponse = new xajaxResponse();
    $objResponse->callbackValue("Hello World");
    return $objResponse;    
    
}

Re: assigning values to a callback function

With xajax 0.5 beta 3 (currently in SVN)... you can already do this with the call parameters object:

myCallback = xajax.callback.create();
myCallback.onComplete = function(oRequest) {
    alert(oRequest.myOwnParameter);
}
xajax.call('myFunction', { callback: myCallback, myOwnParameter: 'test' } );

Or something along those lines.  Does this help or am I missing something?

// Joe

xajax Developer
Connect to me on LinkedIn:
http://www.linkedin.com/in/calledtoconstruct

Re: assigning values to a callback function

CtC wrote:

xajax.call('myFunction', { callback: myCallback, myOwnParameter: 'test' } );

Or something along those lines.  Does this help or am I missing something?

// Joe

I know the callback functions from 0.5b2 but what I want to assign any parameters from php to the callbackfunction on clientside directly. wink I don't want to parse the request-object myself.
Or am I missing something? wink

Re: assigning values to a callback function

Sorry, I guess my example didn't fully address the issue (saving context on the browser side, while also incorporating values from the server side)... I'll try again:

myCallback = xajax.callback.create();
myCallback.onComplete = function(oRequest) {
    if (oRequest.myClientParameter)
        alert(oRequest.myClientParameter);
    if (this.myServerParameter)
        alert(this.myServerParameter);
}
xajax.call('myFunction', { callback: myCallback, myClientParameter: 'value from client...' } );

with:

function myFunction() {
    $objResponse = new xajaxResponse();
    $objResponse->script("myCallback.myServerParameter = 'value from server...';");
    return $objResponse;
}

The key is... the callback functions can take a parameter (the oRequest) which can be used to maintain browser state from request to response, regardless of whether it is synchronous or asynchronous.

// Joe

xajax Developer
Connect to me on LinkedIn:
http://www.linkedin.com/in/calledtoconstruct

Re: assigning values to a callback function

Thanks for you reply.. I guess I understand what you mean, but that solution doesn't satisfy me at all smile sorry.

CtC wrote:

Sorry, I guess my example didn't fully address the issue (saving context on the browser side, while also incorporating values from the server side)... I'll try again:

myCallback = xajax.callback.create();
myCallback.onComplete = function(oRequest) {
    if (oRequest.myClientParameter)
        alert(oRequest.myClientParameter);
    if (this.myServerParameter)
        alert(this.myServerParameter);
}
xajax.call('myFunction', { callback: myCallback, myClientParameter: 'value from client...' } );

In my opinion this is more like a kind of "wrapper" with callback functionality, but I need to set the callbackfunction diretly without any indirect ways.


CtC wrote:

with:

function myFunction() {
    $objResponse = new xajaxResponse();
    $objResponse->script("myCallback.myServerParameter = 'value from server...';");
    return $objResponse;
}

The key is... the callback functions can take a parameter (the oRequest) which can be used to maintain browser state from request to response, regardless of whether it is synchronous or asynchronous.

// Joe

and that's the second point that doesn't satisfy me for a couple of reasons..

$objResponse->script("myCallback.myServerParameter = 'value from server...';");

This call requires valid JavaScript code. My solution can take any PHP variables and the xajax engine would convert it into JavaScript. I don't want to care about the JS syntax inside php - coders are lazy smile

The next reason is, that I sometimes want set functions as callback, that I cannot modify to make it compatible with your solution. I'm using ExtJs for example and I want to call functions from "build-in" classes without loosing the scope.

I used my "hack" in some of functions and it's working great - it makes my life much easier smile Each xajax request acts like a single function-call, no need to setup any callback classes around the call. Just "make an xajax request and send the results to this.anotherClass.updateAnElement(value_from_Server);" - nothing more smile  In my eyes, this is more lightweight...

I don't know how to explain it better - my english knowledge is too limited to explain all the details, sorry. But I hope you understand me anyway...