Welcome Guest, Not a member yet? Register   Sign In
Xajax 0.5.3 in Code Igniter 1.5.4
#1

[eluser]Leonardo Radoiu[/eluser]
Hello!

For everyone out there who may need some help implementing Xajax 0.5.3 in the latest version of Code Igniter here is an example. First of all the xajax library file that will be loaded:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Xajax_lib {
function Xajax_lib() {
    require_once(BASEPATH.'/libraries/xajax_core/xajax.inc'.EXT);
}
}
?>

You need to place folder "xajax_core" from the Xajax 0.5.3 kit in the system/libraries folder of the Code Igniter framework.
Now, let's say we have a list of items and we want to fill a select box with this list, dinamically, using Xajax. We create a model named Item with a method that selects the items using, let's assume again, a webservice:

Code:
<?php
class Item extends Model {

var $_id = null;
var $_start = null;
var $_limit = null;
var $_sortField = null;
var $_sortOrder = null;

function __construct() {
    parent::Model();
        
    $this->load->library('Xajax_lib');
        $this->load->library('Nusoap_lib'); // see http://ellislab.com/forums/viewthread/59710/
}
    
function select_items($params) {
    $items = array();
        
    $objResponse = new xajaxResponse();
            
    $params['start'] = isset($params['start'])?$params['start']:0;
    $params['limit'] = isset($params['limit'])?$params['limit']:10;
    $params['sortField'] = isset($params['sortField'])?$params['sortField']:"title";
    $params['sortOrder'] = isset($params['sortOrder'])?$params['sortOrder']:"ASC";

    //we assume we have a web service located here:

    $this->soapclient = new soapclient('http://localhost/webservice.php/ItemWSVC/select_items/wsdl');
        
    if(!$this->soapclient->fault) {
        if(!$this->soapclient->getError()) {
            $templates = $this->soapclient->call (
            'selectItems',
            array($params['start'], $params['limit'], $params['sortField'], $params['sortOrder']),
            "urn:ItemWSDL",
            "urn:ItemWSDL#selectItems"
            );
        }
    }

    $script = "var items = [];\r\n";

    foreach($items as $key=>$value) {
        $script .= "items.push({id: ".$value['id'].", title: '".$value['title']."'});\r\n";    
    }

    $script .= "Item_SelectItems(items);\r\n"; // the js function we execute at the end
    $objResponse->script($script);
    return $objResponse;
}
}
?>

Please observe we don't register this function here. We'll do that later, below.
The js code for function Item_SelectItems (you need to load this function in the head section of the view in the xajax.js file):

Code:
/* I use jquery.selectboxes.js plugin to fill the select box */
function Item_SelectItems(items) {
    for (i = 0; i < items.length; i++) {
        $('#itemId').addOption(items[i].id, items[i].title);
    }
    $('#itemId').selectOptions("0");
}

Now, we need a controller named mypage.php that registers the xajax function:

Code:
class MyPage extends Controller {
function __construct() {
    parent::Controller();

    $this->load->model('Item');
    $this->load->library('Xajax_lib');
}

function index() {
        $this->xajax = new xajax();

        $this->xajax->registerFunction(array('select_items', &$this->Item, 'select_items'));
        $this->xajax->processRequest();

        $this->view->load('mypage');
}

Finally, the view named mypage.php that holds the form field and calls the js-fied xajax function.

Code:
&lt;html&gt;
&lt;head&gt;
&lt;script src="&lt;?php echo $this->config->item('base_url'); ?&gt;/js/jquery.js"&gt;&lt;/script&gt;
&lt;script src="&lt;?php echo $this->config->item('base_url'); ?&gt;/js/jquery.selectboxes.js"&gt;&lt;/script&gt;
&lt;script src="&lt;?php echo $this->config->item('base_url'); ?&gt;/js/xajax.js"&gt;&lt;/script&gt;
&lt;?php isset($this->xajax)?$this->xajax->printJavascript($this->config->item('base_url').'/js'):""; ?&gt;
&lt;/head&gt;

&lt;body&gt;

<select id="itemId" name="itemId">
    <option value="0">all items</option>
</select>

&lt;script type="text/javascript"&gt;
//&lt;![CDATA[
    var params = new Object();
    params.start = 0;
    params.limit = 10;
    params.sortField = "title";
    params.sortOrder = "ASC";
    xajax_select_items(params);
//]]>
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;

In the end I attach the code for the ItemWSVC web service:

Code:
class ItemWSVC extends Controller {
function __construct() {
    parent::Controller();

    $this->load->library("Nusoap_lib");

    $this->nusoap_server = new soap_server();
    $this->nusoap_server->configureWSDL("ItemWSDL", "urn:ItemWSDL");
    
    $this->nusoap_server->wsdl->addComplexType(
        "ItemsRecordset",
        "complexType",
        "array",
        "",
        "SOAP-ENC:Array",
        array(
            "id"=>array("name"=>"id", "type"=>"xsd:int"),
            "title"=>array("name"=>"title", "type"=>"xsd:string"),
            "description"=>array("name"=>"description", "type"=>"xsd:string")
        )
    );

    $this->nusoap_server->register(
        "selectItems",
        array("start" => "xsd:int", "limit" => "xsd:int", "sortField" => "xsd:string", "sortOrder" => "xsd:string"),
        array("return"=>"tns:ItemsRecordset"),
        "urn:ItemsWSDL",
        "urn:ItemsWSDL#selectItems",
        "rpc",
        "encoded",
        "Retrieves items recordset"
    );
}

function index() {
   if($this->uri->rsegment(3) == "wsdl") {
    $_SERVER['QUERY_STRING'] = "wsdl";
   } else {
    $_SERVER['QUERY_STRING'] = "";
   }

   $this->nusoap_server->service(file_get_contents("php://input"));
}
    
function select_items() {
   function selectItems($start, $limit, $sortField, $sortOrder) {
       // your code
   }
}
}


Messages In This Thread
Xajax 0.5.3 in Code Igniter 1.5.4 - by El Forum - 08-30-2007, 04:50 PM
Xajax 0.5.3 in Code Igniter 1.5.4 - by El Forum - 09-14-2007, 12:49 PM
Xajax 0.5.3 in Code Igniter 1.5.4 - by El Forum - 09-14-2007, 01:09 PM
Xajax 0.5.3 in Code Igniter 1.5.4 - by El Forum - 09-14-2007, 01:17 PM
Xajax 0.5.3 in Code Igniter 1.5.4 - by El Forum - 09-14-2007, 04:03 PM
Xajax 0.5.3 in Code Igniter 1.5.4 - by El Forum - 09-14-2007, 04:13 PM
Xajax 0.5.3 in Code Igniter 1.5.4 - by El Forum - 09-15-2007, 11:10 AM
Xajax 0.5.3 in Code Igniter 1.5.4 - by El Forum - 09-15-2007, 11:32 PM
Xajax 0.5.3 in Code Igniter 1.5.4 - by El Forum - 09-16-2007, 01:51 PM
Xajax 0.5.3 in Code Igniter 1.5.4 - by El Forum - 05-13-2009, 02:01 PM
Xajax 0.5.3 in Code Igniter 1.5.4 - by El Forum - 05-13-2009, 02:20 PM



Theme © iAndrew 2016 - Forum software by © MyBB