I have a PHP application that is built around the MVC architecture without any frameworks that currently generates full pages.
For example: Visiting (http://site.com/xyz) would generate the full page and deliver it to the browser.
I now want to add AJAX into the mix, so that only part of the page is returned via an AJAX response. For the above example, it is quite easy to implement.
However, the application is hierachical in the sense that there can be many sub pages and pages within subpages.
In my case, let's take this page for example: http://site.com/system/backups/databases
The the above case, the following should occur:
If the user is on site.com, the application should generate system/backups/databases and return it in the AJAX response.
If the user is on site.com/system, the application should generate backups/databases and return it.
... and so on.
I am using YUI3 as my javascript framework and would like to minimize any code duplication. How should I deal with the client side?
Should I have a central AJAX component that does ALL the ajax for loading, displaying and unloading pages?
Or should I have a main AJAX component that only deals with loading one level (in this case /system/
) and let the page loaded by /system/
handle the loading of its subpages? This approach seems like it will result in a fair bit of code duplication, but I could be wrong.
On the server side, how should this be handled? How should the server decide whether to render system/backups/databases
or just backups/databases
or databases
?
The solution I came up with was to just send all the HTML over to the client. I have made sure that my HTML is structured properly with IDs and classes to indicate a "sub page".
With this information, the client then knows what is already displayed on the page and what has been received. It then simply removes the parts that are different to the ones receieved and inserts the new parts.
Obviously, this is somewhat inefficient, as we are sending the full response (instead of just the required response) over the wire, but this reduces a lot of logic to sync the client views with that is generated on the server side.