Topic: Calling XAJAX in class functions

I am having a bit of trouble using XAJAX inside classes.  When I try to register a function, it either tells me there is no function by that name or I am out of object.  I have been trying to use ...

$objAjax = new xajax();
$objAjax->registerFunction($this->addSectionData);
$objAjax->processRequests();

As I normally would.  I am not including any other file, the class is in the same file (first part of file is just switches, class is called before switches then the class follows).  Is there a specific way this must be handled?

2 (edited by scourgen 2008-01-03 3:28:01 AM)

Re: Calling XAJAX in class functions

$a=new you_class();

in $a:
$this->ajax = new xajax();
$this->ajax->registerFunction(
array("myFunctionName", &$myObject, "myMethod"));
);

Re: Calling XAJAX in class functions

Since xajax is designed to be used "one xajax object per page" you may want to create a single xajax object, then access it in your classes by passing it in the constructor:

$xajax = new xajax();
$xajax->configure('setting', 'value');
...
$xajax->register(XAJAX_FUNCTION, 'name');
...

$myObject = new MyClass($xajax);
$myOtherObject = new MyOtherClass($xajax, 1000, 50);
...

$xajax->register(XAJAX_FUNCTION, new xajaxUserFunction('someName', &$myObject, 'method'));
$xajax->register(XAJAX_FUNCTION, new xajaxUserFunction('someOtherName', &$myOtherObject, 'method'));
...

class MyClass {
    function MyClass(&$xajax) { ... }
    function method() { ... }
    ...
}

class MyOtherClass {
    function MyOtherClass(&$xajax, $value, $timeout) { ... }
    function method() { ... }
    ...
}

// Joe

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

Re: Calling XAJAX in class functions

Many thanks, this takes care of it.

Re: Calling XAJAX in class functions

Hello,
When I use this way to develop my class method, when I call it, in debug mode I have got this message :
 
ERROR: No response processor is available to process the response from the server.Content-Type: text/html.Check for error messages from the server.

I tried to create new xajax object in my class, but it doesn't work to.

Have I to declare my xajaxResponse specially ?

I've tried to do that :

classe myClass{
       function test(){
       $objResponse = new xajaxResponse();
    $objResponse->script("alert('Hello')");
    return $objResponse;
       }
}

It's just a test, the function should display me a hello, but it doesn't.

Could you help me ?

Thanks

Julien

6 (edited by totigo 2008-01-18 11:34:44 PM)

Re: Calling XAJAX in class functions

hi

open your xajax.inc.php file and sets the character encoding that will be used for the HTTP output :

Searh this :  if (!defined ('XAJAX_DEFAULT_CHAR_ENCODING')) define ('XAJAX_DEFAULT_CHAR_ENCODING', 'UTF-8');

Use :  ISO-8859-1  for latin content  or  UTF-8  (8-bit Unicode Transformation Format)

Regards!

Rodrigo Solorzano Pardo
Great Software Creations

Re: Calling XAJAX in class functions

Please... avoid changing the xajax file, instead simply:

define('XAJAX_DEFAULT_CHAR_ENCODING', 'whatever you want here');

before including xajax:

include $xajax_root . 'xajax.inc.php';

// Joe

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

8 (edited by damian 2008-08-03 12:39:01 PM)

Re: Calling XAJAX in class functions

I have similar problem, but instead of registering a function I would like to register an event handler.

How do i have to do that?

Thanks for your help.

Damian

Re: Calling XAJAX in class functions

It should work much the same way:

[code=php]
$xajax = new xajax();

class Foobar {
    function method() { ... }
}

$myObject = new Foobar();

...

$xajax->register(XAJAX_EVENT, 'eventName');
$xajax->register(XAJAX_EVENT_HANDLER, new xajaxUserFunction('someName', &$myObject, 'method'));

...

$xajax->processRequest();
[/code]

Now you can call xajax_eventName(); which will trigger a call to $myObject->method()

// Joe

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

Re: Calling XAJAX in class functions

I only started recently with xajax and I like it very much.

Thank you very much, but my problem is actually a bit more complicated.  I have to following class :

<?php

class DcTreeViewLine extends clsTable {

    private $_xajax;

    public function __construct($xajax, $m_item, $is_last)
    {
        $this->_xajax = $xajax;

        parent::clsTable();

        $this->setAttribute('id', 'treTable');
        $this->setAttribute('width', '100%');
        $this->setAttribute('border', '0');
        $this->setAttribute('cellspacing', '0');
        $this->setAttribute('cellpadding', '0');
        $this->setAttribute('id', 'TREE_VIEW_LINE_' . $m_item->getItemId());

        $tbody = new clsTbody();

        $tr =& new clsTr();
        $tr->setAttribute('valign', 'middle');

        $this->labelText($m_item, $tr);

        $tbody->addChild($tr);

        $this->addChild($tbody);
    }

    private function labelText($m_item, $tr) {

        $label = new clsLiteral($m_item->getLabel());

        $button = new clsButton();
        $button->setAttribute('class', 'item-not-selected');
        $button->setAttribute('id', 'TREE_VIEW_LINE_LABEL_' . $m_item->getItemId());
        $button->addChild($label);

        $button->setEvent('onclick', $event_DeTreeViewLine_itemSelected,
            array(
                array(0, XAJAX_QUOTED_VALUE, $m_item->getItemId()),
                array(1, XAJAX_QUOTED_VALUE, 'event_DeTreeViewLine_itemSelected')
            )
        );

        $td =& new clsTd();
        $td->setAttribute('align', 'left');
        $td->setAttribute('valign', 'middle');

        $td->addChild($button);
        $tr->addChild($td);
    }
}

Now I would like that a function which will be defined inside this class will be called when the user chicks on the button which is created inside the "function labelText".

How do I have to approach that problem?

Thank you very much for your help.

Re: Calling XAJAX in class functions

I certainly can appreciate the issue; thank you for providing the additional information and sample code.  I think you will want to take a look at:

xajax/tests/suite/index.php

This script uses a similar technique.  You can define your event handler methods in DcTreeViewLine, then register the entire class with:

$this->aRequests =& $xajax->register(XAJAX_CALLABLE_OBJECT, $this);
(from the constructor?)
(don't forget to add a member called aRequests)

Then, you can set the button event like so:

[code=php]
    public function OnItemSelected($itemID) { ... }
[/code]
[code=php]
    private function labelText($m_item, $tr) {
        ...
        $this->aRequests['onitemselected']->setParameter(0, XAJAX_QUOTED_VALUE, $m_item->getItemId());
        $button->setEvent('onclick', $this->aRequests['onitemselected']);
        ...
    }
[/code]

NOTES:  The OnItemSelected method is intentionally public and referenced in all lower case on purpose (because PHP4 converts function names to lowercase)

// Joe

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