CodeIgniter Forums
calling the right method after ajax call - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: calling the right method after ajax call (/thread-15790.html)

Pages: 1 2


calling the right method after ajax call - El Forum - 02-15-2009

[eluser]stoefln[/eluser]
hi,

i m calling a method "deleteImages" by ajax in one of my controllers.
after deleting the images i want to return a refreshed view of the list of images.
now i have the problem that the method "deleteImages" can be called from different locations (different views). how can i determine where the ajax call was made from and what view therefore should be chosen?


calling the right method after ajax call - El Forum - 02-15-2009

[eluser]pistolPete[/eluser]
You can send an additional parameter in your ajax call containing the current "location".


calling the right method after ajax call - El Forum - 02-15-2009

[eluser]stoefln[/eluser]
maybe my question is not as complete as it should. determining the location is acutally not a big problem: i can call $this->uri->segment(3) to get the method where the current view was rendered. but i have to do a switch($this->uri->segment(3)), that means i have to do a manually routing of methods and parameters. thats something what CI usually does for me. isnt there a more generic way, maybee using the routing functionallity of CI?


calling the right method after ajax call - El Forum - 02-15-2009

[eluser]pistolPete[/eluser]
Could you please provide some example code of your controller and view?
What needs to be different in the response to the ajax call?
What parameters do your ajax requests contain?


calling the right method after ajax call - El Forum - 02-15-2009

[eluser]TheFuzzy0ne[/eluser]
I'm not sure I understand the problem. Each controller should be aware of it's location due the the controllers name.

Code:
class Test_controller extends Controller {
    
    function Test_controller()
    {
        parent::Controller();
    }
    
    function index()
    {
       // I am the index method, and I load the index view.
    }

    function get_images()
    {
        // I am the get_images method, and I load the get_images view.
        ...
    }
}

Hopefully you see what I am getting at. Each controller method is aware of its purpose. Location is irrelevant. Please could you give an example of what you're trying to achieve?


calling the right method after ajax call - El Forum - 02-15-2009

[eluser]stoefln[/eluser]
the problem is that i call a method "deleteImages" in my controller by AJAX. after deleting the images i want to retrieve the html of the controller-method which is linked to the url where i got from.
Code:
class Test_controller extends Controller {
    
    function Test_controller()
    {
        parent::Controller();
    }
    
    function index()
    {
       // I am the index method, and I load the index view.
    }

    function get_images_by_category()
    {
        // I am the get_images method, and I load the get_images view.
        ...
    }
    function get_images_by_user()
    {
        // I am the get_images method, and I load the get_images view.
        ...
    }
    function get_images_by_date()
    {
        // I am the get_images method, and I load the get_images view.
        ...
    }
    function delete_images($imageIds)
    {
        deleteImages();
        switch($this->uri->segment(3)){
           case "get_images_by_user":
             html = $this->get_images_by_user(params...);
             break;
           case "get_images_by_date":
             html = $this->get_images_by_date(otherParams...);
             break;
           case "get_images_by_category":
             html = $this->get_images_by_category(params...);
             break;
           case "whatever"...
        }
        // return ajax response
        return html;
    }
}
what i m trying to say is that i have to rebuild the whole routing algorithm by a switch, and i think thats not very sexy. so i was wondering if there is another way...


calling the right method after ajax call - El Forum - 02-15-2009

[eluser]pistolPete[/eluser]
I assume that your third uri-segment really contains the functions names, then you can use something like that:
Code:
function delete_images($imageIds)
    {
        deleteImages();
        $function = $this->uri->segment(3);
        $html = $this->$function(params...);
        // return ajax response
        return $html;
    }

Have a look at Variable functions.


calling the right method after ajax call - El Forum - 02-15-2009

[eluser]stoefln[/eluser]
Yeah, but the methods have different parameter sets- so i would need a switch in this case too... another issue is that the user could call EVERY method in the controller


calling the right method after ajax call - El Forum - 02-15-2009

[eluser]TheFuzzy0ne[/eluser]
Not if you're running PHP 5 and you make the private methods. Or in fact, probably better yet, prefix the functions with an underscore if you don't want them directly accessible from the outside.


calling the right method after ajax call - El Forum - 02-15-2009

[eluser]pistolPete[/eluser]
Another thought:
Why do you have to respond with the updated image list?
You could only respond with (e.g.) a JSON "true"/"false" success message, then the ajax script would react and reload the image list by directly calling the appropriate controller function.