Welcome Guest, Not a member yet? Register   Sign In
[Solved] CI + XAJAX Global Functions
#1

[eluser]LifeSteala[/eluser]
Hi
I am developing an app for work. I have many controllers needing the same xajax function I wrote. I tried to put these functions in my MY_Controller.php file (gets extended on every controller), but I don't think these functions are accessible (cause it's not working).

I register these functions in the controllers index(). Do I need to register them in My_Controller.php??

How can I globalize these AJAX functions I wrote. I want to be able to re-use them.

Thanks
#2

[eluser]geckzilla[/eluser]
I think what you want to do is put them in a library instead of extending the controller, say you call it "Ajax". Then in the autoload config add your library to the list and all your functions will be available via $this->ajax->some_function()
#3

[eluser]LifeSteala[/eluser]
OK I could give that a go. What happens is I have a select list which a user chooses an option. On choosing that option, it auto populates the second select list like this.

Code:
onChange=\"xajax_get_tasks(this[this.selectedIndex].value);


I tried putting into a library. It still seems like it cannot access those functions.

Any ideas?
#4

[eluser]geckzilla[/eluser]
Isn't that javascript?
#5

[eluser]LifeSteala[/eluser]
Yeah, that is on the HTML side. If I put the function inside the actual controller, that JavaScript can access the function and do what I need it to do.

I put that function in a library as you mentioned, and loaded it, however it still doesn't work.

So I'm guessing that JavaScript cannot access the function inside the loaded library?
#6

[eluser]LifeSteala[/eluser]
So is it possible to re-use a AJAX function across many controllers?
#7

[eluser]geckzilla[/eluser]
You just need to include the javascript file in your template header... codeigniter is php only.
#8

[eluser]LifeSteala[/eluser]
I've included the JavaScript. I register the functions in the controllers constructor. Here's my controller.

Code:
class Entertime extends MY_Controller
{
    function Entertime()
    {
        parent::MY_Controller();
        
        $this->load->model('timesheets');
        $this->load->model('clients');
        $this->load->model('projects');
        $this->load->model('tasks');
        
        $this->load->library(array('validation', 'xajax', 'globalajax'));
        
        $this->load->helper('url');
        $displayoutput = "";
        $type = "";
        $name = "";
        $status = "";
    }
    
    
    function index()
    {

        $this->xajax->registerFunction(array('get_projects',&$this,'get_projects'));
        $this->xajax->registerFunction(array('get_tasks',&$this,'get_tasks'));
        $this->xajax->registerFunction(array("auto_suggest", &$this, "auto_suggest"));
        $this->xajax->processRequest();
        $data['xajax_js'] = $this->xajax->getJavascript(base_url());
                
                // More code here
        }
}

Here's my library

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

    class Globalajax
    {
    
        function Globalajax()
        {
            log_message('debug', "Global Ajax Functions Class Initialized");
        }

        // Function: get_projects - Public
        // Gets projects depending on what client is selected. AJAX
        //-----------------------------------------------------------------------------------------------
        function get_projects($clientName)
        {
            $proj_query = $this->projects->getProjectsAjaxSearch($clientName);
            
            $output = '<label for="project">Project: </label>';
            $output .= "<select name=\"project\">\n";
            
            if ( $proj_query->num_rows() > 0 ) {
                $output .= '<option selected="selected"></option>';
                
                foreach($proj_query->result() as $rows)
                    $output .= "<option value=\"{$rows->project_id}\">{$rows->project_name}</option>";
            }
            else
                $output .= '<option selected="selected">No projects available</option>';
            
            $output .= '</select>';
            
            $objResponse = new xajaxResponse();
            $objResponse->Assign("optionProjects","innerHTML", $output);
            return $objResponse;
        }
    
        // Function: get_tasks - Public
        // Gets tasks depending on what project is selected. AJAX
        //-----------------------------------------------------------------------------------------------
        function get_tasks($projectID)
        {
            $task_query = $this->tasks->getTasksAjax($projectID);
            
            $output = '<label for="task">Task: </label>';
            $output .= '<select name="task">\n';
            
            if ( $task_query->num_rows() > 0 ) {
                $output .= '<option selected="selected"></option>';
                
                foreach($task_query->result() as $rows)
                    $output .= "<option value=\"{$rows->task_id}\">{$rows->task_name}</option>";
            }
            else
                $output .= '<option selected="selected">No tasks created</option>';
            
            $output .= '</select>';
            
            $objResponse = new xajaxResponse();
            $objResponse->Assign("optionTasks","innerHTML", $output);
            return $objResponse;
        }
        
        // Function: get_tasks - Public
        // Gets tasks depending on what project is selected. AJAX
        //-----------------------------------------------------------------------------------------------
        function auto_suggest($val)
        {
            $objResponse = new xajaxResponse();
    
            $result = "";
    
            if (isset($val) && $val != "")
            {
                $query = $this->clients->searchClients($val);
    
                if ($query->num_rows() > 0) {
                    foreach ($query->result() as $row) {
                        $result .= "<div title='". $row->client_name . "' onmouseover='[removed]suggest_over(this);'";
                        $result .= " onmouseout='[removed]suggest_out(this);'";
                        $result .= " onclick='[removed]set_search(this.title);' class='suggest_link'>";
                        $result .= $row->client_name;
                        $result .= "</div>";
                    }
    
                    $objResponse->Assign("client_result", "style.display", 'block');
                    $objResponse->Assign("client_result", "innerHTML", $result);
                }    else {
                    $objResponse->Assign("client_result", "style.display", 'none');
                }
            }    else {
                $objResponse->Assign("client_result", "style.display", 'none');
            }
    
            return $objResponse;
        }
    }
?&gt;
#9

[eluser]Sumon[/eluser]
Well as though you have
[code]
class Globalajax
{
/// Your codes.....
function Globalajax()
{
log_message('debug', "Global Ajax Functions Class Initialized");
}
function get_tasks($projectID)
{
//Generate List drop down
}
}
[code]
it's possible to call this function inside javascript function like yoursite.com/globalajax/get_tasks(5)
#10

[eluser]LifeSteala[/eluser]
I have managed to solve this using MY_controller.

I created a new function called load_ajax() inside MY_controller. In this function I register the ajax functions and process the request.

The actual controller then calls load_ajax() and then loads the JavaScript.

Thanks to those who helped.




Theme © iAndrew 2016 - Forum software by © MyBB