• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
organizing your ajax functions in code igniter?

#1
[eluser]edwardmolasses[/eluser]
hi everyone,

This might be a silly question but i'm curious how people are organizing their ajax functions in code igniter?

What i've been doing is putting my jQuery ajax calls in my views, and then they will call a corresponding function in the controller that i usually prefix with 'ajax' and then those functions will call whatever model functions they need.

Unfortunately, i find as the application grows this starts to look a bit disorganized and maybe hard to read as the ajax functions are mixed in with all the other functions, so i was wondering if anyone has some cleaner organizational schemes they might like share?

thanks!

#2
[eluser]Ivan A. Zenteno[/eluser]
I use

view_controller_name() for views

action_controller_name() for calls ajax

in my routes using

$route['([a-z]+)/action/([a-z]+)'] = "$1/action_$2";

localhost/module/action/controller for my url's

maybe help you my scheme

#3
[eluser]edwardmolasses[/eluser]
[quote author="Ivan A. Zenteno" date="1232434424"]I use

view_controller_name() for views

action_controller_name() for calls ajax

in my routes using

$route['([a-z]+)/action/([a-z]+)'] = "$1/action_$2";

localhost/module/action/controller for my url's

maybe help you my scheme[/quote]

that's a neat idea! I might try that one thank you. Smile

#4
[eluser]Michael Wales[/eluser]
Since I am a stickler for standards and graceful degredation, I do the following:

1. Build my app with Javascript turned off, ensure it works.
2. Add in my jQuery shenanigans.
3. Modify the original controllers to handle requests appropriately if that controller was called via AJAX.
4. Profit.

#5
[eluser]Colin Williams[/eluser]
I like #4, Michael. Need to work on that one myself... Smile

#6
[eluser]Michael;[/eluser]
@Michael: Being new to javascript in general, okay, more like a complete n00b, I'm curious what you do to actually make a controller aware if it was called via ajax? Currently I use a controller that simply handles ajax requests and a series of plugins and helpers that contain the functions called. I'm thinking it would be sooo much more organized to have all the functions related to a specific controller actually *IN* the controller instead of a plugin or helper file.

Michael;

#7
[eluser]Colin Williams[/eluser]
Code:
if
(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')
{
   // It's ajax
}

#8
[eluser]Michael;[/eluser]
Collin,

So with that snippet I could just put my code in the construct and return whathaveyou without a separate ajax controller?

Wow ... that means I don't need ... or ... and I can eliminate ...

Oh WOW.

*giggles like a school girl*

*runs off to write code*

#9
[eluser]edwardmolasses[/eluser]
Thanks everyone, i'm especially excited by the last tip by colin. I am also giggling like a schoolgirl now Smile

#10
[eluser]Colin Williams[/eluser]
Horray for HTTP headers! Horray!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.