Welcome Guest, Not a member yet? Register   Sign In
AJAX & CI Controllers
#1

[eluser]gte451f[/eluser]
Hi Gang,

I've run into a design question that has stumped me so I wanted to put my question out to your collective wisdom.

I've designed a CI app where a web page will send AJAX requests to various controllers to perform CRUD operations and return HTML snippets that refresh the calling web page.

So if someone hits the delete button, ajax hits a delete function to:
1)remove the record and
2) return HTML snippit with which to update the original page.

The problem I'm running into is that I find my self on one page but need to call CRUD functions associated with another controller yet return HTML from the original calling controller.


So how to resolve this issue?

Can I call a controller from inside another controller?
I hear this is a bad idea and I'll be branded an MVC heretic. That and CI won't do this, that I know.

Must I put view related logic in a separate library?
In which case, my controllers are getting pretty insignificant if they can't even marshal data and return views since I may need to use that logic from a variety of controllers.

Should I never make ajax calls to controllers different than the controller that rendered a page?
That's gonna make form some large Frankenstein like controllers where they manage all sorts of CRUD operations for various tables even if they are just kicking the work off to underlying models.

How about 2 Ajax calls? One for CRUD to whatever controller followed by a 2nd ajax call to refresh the original page.
meh...this one is slightly better, but now I make 2 ajax calls everytime I need to do something on a web page.

Ok, let's test out the wisdom of crowds. Guide me oh wise CI community!
#2

[eluser]boltsabre[/eluser]
Maybe not ideal solution, but could you not just have a "ajax" method inside your controller?

So, the user hits 'delete', ajax calls the 'ajax delete' method in your controller, which in turns calls the model and 'delete' method. Upon successful return from the model you pass the 'deleted id' from your ajax controller back into the view and "fadeOut" the appropriate wrapper div/p/whatever for that record?

There is a way to "lock" controller methods from non-ajax requests, meaning that someone cannot type the url into their browser and call your ajax delete function (which could have devastating impacts if they could)! I've forgotten how to do it (I'm at work and don't have my CI projects infront of me), so you'll have to research this part yourself.
If I remember correctly, there is a line of code that you place inside your config somewhere which checks for a ajax request, and sets a variable (true/false). Then the first thing you do inside your 'ajax' controller is check this variable, if not true, throw up a 404 page and 404 http header, thus preventing people calling it from the direct from the url.

Good luck, post back so we can see what you come up with!




Theme © iAndrew 2016 - Forum software by © MyBB