EllisLab text mark
Advanced Search
     
Where do you put your AJAX stuff?
Posted: 12 November 2009 08:53 AM
Joined: 2009-05-21
103 posts

Before CI, I used to have one file “ajaxStuff.php” which handled all of my ajax calls.

Now in this MVC architecture of CI, where is the best place to put them? Create a controller just for ajax or within the controller the call relates to?

 
Posted: 12 November 2009 09:57 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2008-06-05
333 posts

I just call Ajax calls from my views , but calling it through controller is good option

 Signature 

Zeeshan RasooL
Lahore - Pakistan
http://www.99Points.info


Facebook Wall Script Facebook Style Extract URL data with JQuery Ajax Rating System Script Who Am I

 
Posted: 12 November 2009 10:21 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2009-05-21
103 posts

Hi Thanks for the reply!


I’m talking about the response to the ajax call. What do you do for that?

 
Posted: 12 November 2009 11:51 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2009-03-10
1388 posts

The controller/method that is appropriate for that request?

 Signature 

Isset | Isset Public Code Repo | Simple Message Library | Session Profiler for CI2.0 | CI session issues in IE

 
Posted: 12 November 2009 01:02 PM   [ # 4 ]   [ Rating: 0 ]
Joined: 2009-05-21
103 posts

Cool… I’ve got a method called availability, and I’ve set up another method called availabilityAjax.

I know there’s many ways to skin a cat.. I just want to make sure i’m using the best way ! smile

 
Posted: 12 November 2009 01:08 PM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2008-05-09
314 posts

the way i usually do it is extend the controller class (MY_Controller) to have a method to check if the call was AJAX based.

e.g.

function _is_ajax() 
    
{
        
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'== 'XMLHttpRequest'));
    

and then in the relevant controller/method i can take appropriate action
e.g.

function sample_method() 
    
{
        
if($this->_is_ajax())
        
{
            
// do you ajax thing here    
        
}
        
else
        
{
            
// not an ajax call, reload the page or whatever is needed
        
}
    } 
 Signature 

Starfish Web Consulting Web Design & Development Belfast

Quicksnaps - CI Photo gallery app

 
Posted: 12 November 2009 01:37 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2009-05-21
103 posts

That is effing brilliant! I had no idea that server variable even existed!

 
Posted: 12 November 2009 03:16 PM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2008-05-09
314 posts

glad to be of help.

now just add a sprinkling of magic jquery dust to the mix and you’re good to go wink

 Signature 

Starfish Web Consulting Web Design & Development Belfast

Quicksnaps - CI Photo gallery app

 
Posted: 12 November 2009 04:57 PM   [ # 8 ]   [ Rating: 0 ]
Joined: 2009-05-21
103 posts

jQuery is my new best friend ! I’m still a newbie with jQuery and javascript and am still in “bracket and curly brace hell” but I’ve still got some cool stuff done with it.

Thanks again!

 
Posted: 12 November 2009 09:43 PM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2009-11-12
80 posts

i am using jquery too and other javascript files.
you can put your javascript anywhere, but most preferable make a js folder at the root folder
then put them all.

Note: use base_url in including your scripts.

 Signature 

tourdavao.com