I’m developing a web application using a very simple (maybe not really MVC-compliant) MVC framework, coded by myself while developing the application to keep the code clean.
My application, though, has many AJAX components and now I’m stuck trying to integrate them within the general MVC structure. How should they be integrated?
I have something like this in my Javascript files:
$('#pageList').load(BASE_SITE_URL + 'ajax/pageList.php');
and pageList.php used to have a structure like the following one:
<?php
require '../includes/config.inc.php';
require BASE_PATH . 'includes/init.inc.php';
// a whole load of Controller logic here and then...
echo "<table>";
//display some user data
echo "</table>";
I’m really confused about this, any advice is appreciated
Few pointers – your website should be functional without javascript. For example, if you have pagination with page urls like /list/?page=1 … /list/?page=n then you should make sure all of your pages are clickable without actually needing javascript.
Javascript should really be an extension to your website. In the above example, you can come back and use js to replace the functionality of all the pagination with a simple ajax behavior. What you would probably want to do is use jquery.load to do something like:
Note the selector after the load. This is very important because I haven’t actually created any new HTML pages to make my website AJAX enabled. Instead I use what already is available to the browser as a simple URL and load it using ajax.
Of course, there are times that you need AJAX only content where won’t really exist on a URL. In a situation like this I recommend creating a controller for all your ajax stuff and then rendered each path with a view.
I hope this helps.