Page 1 of 2
Implementing AJAX support in an MVC Framework
Posted: Fri Jun 01, 2007 3:44 pm
by AlexC
Hey there,
I'm wanting to add AJAX support to my MVC framework, however I can not really see it working (with any benefits) - probably down to my lack of knowledge of how it will/can work. I would have to make a request to a certain Controller's section/action correct, and that would be the same URL as if I was going to it directly via the browser? But, when this happens - it would load the entire framework again, and so if I have 1 AJAX call in my application - that is technically calling the entire page twice, and so I will have double the queries and double the load for one page ... which takes away half of the reasoning for AJAX.
I'm really confused about this as you can probably tell, so if anyone could share some tips on how to do this with an MVC framework I'd really appreciate it.
Thanks,
Posted: Fri Jun 01, 2007 4:13 pm
by Ollie Saunders
Run two different front controllers depending on
whether the request is AJAX or not.
Posted: Fri Jun 01, 2007 4:26 pm
by Kieran Huggins
...or call two different sets of templates from the same controllers!
nickvd wrote:Code: Select all
if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') define('IS_AJAX',true);
with something like:
Code: Select all
$template = '/path/to/template.php';
if (IS_AJAX) $template = '/xml'.$template;
Posted: Fri Jun 01, 2007 4:40 pm
by AlexC
That's great, should help a lot! Although, with that - it will still have to make a new MySQL connection and load the entire framework again wont it? Maybe I could use that $_SERVER['HTTP_X_REQUESTED_WITH'] to load an alternative, light-weight bootstrap and have a special ajax-controller class that would load extra parts of the bootstrap and set them up correctly for the ajax-controller ... hum, I have an idea

Posted: Fri Jun 01, 2007 5:03 pm
by Ollie Saunders
I don't see why you would have to load anything twice. Sounds like you need to rethink some of your design there.
Re: Implementing AJAX support in an MVC Framework
Posted: Fri Jun 01, 2007 5:14 pm
by Christopher
AlexC wrote:I'm wanting to add AJAX support to my MVC framework, however I can not really see it working (with any benefits) - probably down to my lack of knowledge of how it will/can work. I would have to make a request to a certain Controller's section/action correct, and that would be the same URL as if I was going to it directly via the browser?
"Ajax support" is a little bit of a vague term as that technology is just an alternate way to make HTTP requests to a server. If you wrote a database results pager using Ajax, the server side code would be identical to one that used a regulat GET request. However I think you are talking about using Ajax to deliver small packets of data to update the page. Given that definition, then you would not use the same controller/action request to create the page initially as you would for succeeding Ajax calls. You could have one or more Ajax actions in your controller or a different controller altogether.
AlexC wrote:But, when this happens - it would load the entire framework again,
The point of implementing a Front Controller is to minimize what is loaded each request -- in a structured way. A Front Controller is no by definition a part of MVC, but it is an elegant way to run controllers.
AlexC wrote:and so if I have 1 AJAX call in my application - that is technically calling the entire page twice, and so I will have double the queries and double the load for one page ... which takes away half of the reasoning for AJAX.,
Again ... you would not have the same code servicing different requests. The goal is to have actions that efficiently serve only the data that is needed. This is achieved by good design of both the client and server code, and with things like caching.
Posted: Sat Jun 02, 2007 1:22 am
by Kieran Huggins
I sort of disagree...
IMO AJAX should be an unobtrusive upgrade to the interface, thus exactly duplicating a standard xhtml request in a slicker, faster way. In that light, the same controllers, security, methods, etc... should be used, but only a subset of data needs to be returned.
In the case of a pagination AJAX call, it should be made as if the user were clicking on the full, AJAX-less url, but redirected to a different "view" template for either XML of JSON, returning only the necessary data. That would skip all the headers, navigation, footers, all ancillary content. This will greatly speed up the transfer and page load times, but should NOT take any server shortcuts.
Besides, it saves you having to write separate controllers and methods for each action while ensuring your security model - it's win/win baby!
Posted: Sat Jun 02, 2007 2:09 am
by Christopher
Kieran Huggins wrote:I sort of disagree...
I think we actually said the same thing in different ways...
Posted: Sat Jun 02, 2007 2:22 am
by John Cartwright
arborint wrote:Kieran Huggins wrote:I sort of disagree...
I think we actually said the same thing in different ways...
I agree with arborint, you've been working too hard Kieran

Posted: Sat Jun 02, 2007 2:33 am
by Christopher
As usual, I sort of agree with Kieran...

Posted: Sat Jun 02, 2007 4:59 am
by AlexC
Hum, now I'm even more confused. I am right in thinking with AJAX, you create another HTTP request and then catch it's output and replace parts of the HTML will the new output? If that's right, then when I make the new request to a URL - it will be acting the same as if I had gone there via the direct URL, and so it would load the entire framework? ... I mean, if one of my AJAX actions/sections requires a MySQL Connection - would it have to make another MySQL connection, or can it use the current one etc?
Would it be possible for some of you to show some examples so I can understand what you're trying to get at (I learn better when seeing code).
Re: Implementing AJAX support in an MVC Framework
Posted: Sat Jun 02, 2007 6:35 am
by kyberfabrikken
AlexC wrote:
But, when this happens - it would load the entire framework again, and so if I have 1 AJAX call in my application - that is technically calling the entire page twice, and so I will have double the queries and double the load for one page
Yes, an ajax'ed page will put a bit more load on your server.
Posted: Sat Jun 02, 2007 9:47 am
by onion2k
AlexC wrote:If that's right, then when I make the new request to a URL - it will be acting the same as if I had gone there via the direct URL, and so it would load the entire framework?
AJAX is essentially a clientside technology. It makes no difference to the number of pages loaded on the server, or how much work they have to do for data wrangling. If your workflow requires 20 steps to do something then it'll be 20 requests to the server whether you use traditional requests or AJAX. The advantage of AJAX however is that you only need to pass the data rather than including the layout as well. That means you're going to be saving bandwidth, CPU time, etc. Plus it's a nicer experience for the user.
But, when this happens - it would load the entire framework again, and so if I have 1 AJAX call in my application - that is technically calling the entire page twice, and so I will have double the queries and double the load for one page
There's nothing stopping you passing the data for the initial setup with the layout and AJAX code that gets loaded on the first hit, so if you write the code properly you won't have to have two requests.
Posted: Sat Jun 02, 2007 10:58 am
by kyberfabrikken
onion2k wrote:
There's nothing stopping you passing the data for the initial setup with the layout and AJAX code that gets loaded on the first hit, so if you write the code properly you won't have to have two requests.
True. Except, that's called javascript -- not ajax.
Posted: Sat Jun 02, 2007 11:42 am
by onion2k
kyberfabrikken wrote:True. Except, that's called javascript -- not ajax.
I have no idea what point you're trying to make.