• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1040
  • Last Modified:

Codeigniter JQuery Ajax

Hello Experts,

I'm a heavy Codeignitor user and currently I have to build a page with extensive AJAX bits and pieces. I have been using JQuery all along the website and it's AJAX handling was perfect up until now. There is something that just doesn't feel right when i use a MVC with JQuery.
For example:
in Jquery I setup the callback URL easily that point to the controller, the controler then calls a view page and the AJAX is displayed but this view page is very small PHP code.
So now I have about 40 ajax function to do in my page, would that mean I would have to make 40 views?? that doesn't seem right to me.
Is there any better way to handle/manage the views created for the AJAX in MVC Frameworks

Thank you.
0
bhefny
Asked:
bhefny
  • 2
  • 2
1 Solution
 
Blue-hitCommented:
If I got you right your problem is, that you don't want to post your Ajax responses via views.

I'm using Zend Framework for php and did it the following way:

I use a single "Ajax Controller" thats catching alle the requests. It consist of one action for each request type (But that are usually between 5 and 10, beacuse you are able to use parameters within them).
The controller creates the JSON or XML and after that I just print it directly from the controller with an echo() and the last line of code inside each controller is a die() to prevent the view of being called.
Taht way I'vve got the the business logic where it should be (controller and model) and no unneccessary views. A couple of echo and die Statements that are conceald in a single controller (what with Zend Framework also means inside a single file) are a cheap price for that if you ask me ;-)
0
 
bhefnyAuthor Commented:
the first half sounds really logical and correct but I personally not in favor of writing HTML in controllers plus sometimes there are a lot of AJAX to be handled and there no point in writing 150 lines of HTML in a <?php echo '....';>

So lets assume we implemented the first half and I have one controller for all.
How can I have one view for all. What I have reached so far is have one view with a switch statment and let the controller pass the any incdentifier variable to it.
Is there anything smarter than what I did?
/*Controller*/
class C_ajax extends Controller {
    
    function fn_name(){
        $data['ajaxType'] = __FUNCTION__;
        // write any code here to be passed
        
        $this->load->view('news/v_news_ajax', $data);
    }
}

/*View*/

<?php
    switch($ajaxType):
        case 'fn_name':?>
            //write HTML here

        <?php break;?>
        <?php case '':?>
            // write HTML for another view here
        <?php break;?>
        
    <?php endswitch;?>
    

Open in new window

0
 
Blue-hitCommented:
You'll need another oppinion here, but I'll add another bit to mine:

I'm not used to codeigniter. Does it support View Helpers? (Taht's the Zend Framewok way for reusing html/css/js code snippets).

My views are actually doing barely more than conacting view helpers (maybe with one or two if/then control structures). That's why I havn't hundreds of lines of html code in my controller ;-)
And some of the view helpers are doing the same thing (view helpers for forms are using vie helpers for input elements in the background).

Maybe you could do something similar. Keep the switch case in the view and call a couple of view helpers from each case. That should keep the view very clean, and you should be able to reuse your html code frequently (if you create view helpers, taht cna be used in more than one case statement).
0
 
bhefnyAuthor Commented:
I don't think the question can should be deleted.
I have provided a solution and someone else proposed a minor adjustment.

This can help someone later on
it's your call
0
 
ee_autoCommented:
Question PAQ'd, 500 points refunded, and stored in the solution database.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now