Welcome Guest, Not a member yet? Register   Sign In
Question regarding MVC and Codegniter with jQuery/Ajax
#1

[eluser]RedFin[/eluser]
Hello CI-Coders,

I'm not native english and i'm pretty bad in describing problems, but i try my best.

I have a very common question about using the MVC to handle jQuery/Ajax requests.

This is my problem:
TabX | TabY | TabZ
ContentX|ContentY|ContentZ

I have a site with different Tabs which have different contents in it. However, i do not want to pre-load all of the content and use #tab.show()/hide(), since the content is pretty heavy and needs some time to load. For example: one content can need up to 2 seconds to load. I have like 5-6 tabs and this can easily need up to 10 seconds loading time.

My solution (not MVC) works like this: if i click a tab, i load the needed php-file via load() and show that file in the content area. all the database actions are inside this php file.

So now i need a solution how to fit this into the mvc pattern. What is the workflow around that? Can i load ControllerY to get the data of ModelY to parse it into ViewY when i click on TabY without reloading the page?

If this doesnt work at all, how would you solve this to have a nice and clean solution?

Kind regards,
John.
#2

[eluser]CroNiX[/eluser]
Sure, but only with javascript.

Set up click events on your tabs (if there aren't any already).

When a tab is clicked, use JS to determine which tab it was and fire off a POST ajax request to a controller sending it the id (or other identifier) of the tab you clicked.

Then in the controller, CI can load the view (only partial...with the actual content...not the whole page) and send it back to your ajax requests' success event.

In the success event, you take the content that was retrieved and replace the HTML content of the div on the current page (or whatever) with the new content.
#3

[eluser]RedFin[/eluser]
by "load the view [...] and send it back to your ajax requests" you mean something like saving the needed source code of the view into a variable (which contains mainly html) and send it back so the stuff can be rendered?




Theme © iAndrew 2016 - Forum software by © MyBB