• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I want to develop a site that has 1 page and many, many AJAX calls

I want to develop a simple site that has only 1 page, but many AJAX calls (using jQuery) that load in content depending on what's being clicked on the page.

From my understanding of MVC, I would create 1 controller to handle the single page. However, how do I handle the content being loaded in via AJAX? Would the content being loaded in be different controllers/views?

What about models? This single page will be loading content from different tables, so would I have a model for each table? Can that model be accessible by multiple controllers?

I understand these may be simple questions, but I come from an asp.net background and simply trying to wrap my head around MVC. I'd really appreciate the help Smile


The content can be loaded from multiple controllers or a single, just depends on how you wanna set it up. Depends on how you want to separate things to keep it clean. Since there will only be a single page, you could technically do it all in one controller since the url wouldn't be changing.

Models, typically (please note the use of this word), are created based on purpose/function. For instance, I would have a single model that would handle stuff that was user related (setting persmissions, registration, login, etc).

A lot of the stuff is just up to you. Personal preference. I personally would split it up into multiple controllers/views, and create models on function rather than which table they are accessing. That's probably what most people here would recommend.

For this I would actually pretend you didn't really have one page - sure, the user will only see 1 url, but in reality if you have a tab for "Create Account" and the user pops over to an account creation form, that's really another page.

If you separate things like this logically you remove the confusion from having one page, and then everything else anyone says about MVC or CodeIgniter applies to you just fine. In your head you just make the association that a certain AJAX state will, to a user and philosophically speaking, be its own page.

You can then think of your AJAX application as a browser, because it kinda is. It is handling the individual pages inside the users browser, like a browser within a browser.

From this it then logically follows that you have controllers to match specific sets of functionality. An accounting software might have Bank_Reconciliation, Periodic_Reporting, Accounts_Receivable, etc. This is more for management and code organization than anything. I still often just use 1 controller because I have small applications - you really only need more if that one controller is going to get too big and unwieldy.

For Models, I use Object Relational Mapping offered by DMZ (Datamapper: OverZealous Extension over in Ignited Code forum), which generally encourages 1 model per table - DMZ provides the CRUD, then you provide all the advanced functionality.

With these semantic agreements and the removal of the extra psychological issue related to an AJAX driven app, I think you'll find MVC much more intelligible.

Just call the URI needed like if it was a "regular" page in your project; expect the controllers' method(s) should not output the document header (<html> ...) and footer.

- index()
load the default page content of your site

- index()
load the default content
- method_a()
echo 'method a called'
- method_b()
echo 'method b called'

call http://domain.TLD/controller_x/method_a using ajax and append the response to the HTML element of your default controller you need to display the response to in.
You can do the methods in your default controller but that would depend how large is your project; hundreds of methods in one file is not going to be easy to manage and all those methods would be loaded for each request to your site.

Thanks a lot for all of the replies guys. I think it's given me a clear idea of where to go.

I think I'll go ahead and start with a single controller and add methods as necessary to that controller. If it starts to get too big, I'll go ahead and refactor the code and separate the methods into different controllers. As for models, I'll try to stick with the method jdfwarrior brought up, as I think that makes a bit more sense than the per-table model method.

Thanks again!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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