Implementing AJAX support in an MVC Framework
Moderator: General Moderators
Implementing AJAX support in an MVC Framework
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,
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,
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Run two different front controllers depending on whether the request is AJAX or not.
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
...or call two different sets of templates from the same controllers!
with something like:nickvd wrote:Code: Select all
if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') define('IS_AJAX',true);
Code: Select all
$template = '/path/to/template.php';
if (IS_AJAX) $template = '/xml'.$template;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 
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Implementing AJAX support in an MVC Framework
"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: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?
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:But, when this happens - it would load the entire framework again,
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.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.,
(#10850)
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
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!
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!
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I agree with arborint, you've been working too hard Kieranarborint wrote:I think we actually said the same thing in different ways...Kieran Huggins wrote:I sort of disagree...
Last edited by John Cartwright on Sun Jun 03, 2007 2:23 pm, edited 1 time in total.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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).
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).
- kyberfabrikken
- Forum Commoner
- Posts: 84
- Joined: Tue Jul 20, 2004 10:27 am
Re: Implementing AJAX support in an MVC Framework
Yes, an ajax'ed page will put a bit more load on your server.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
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.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?
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.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
- kyberfabrikken
- Forum Commoner
- Posts: 84
- Joined: Tue Jul 20, 2004 10:27 am
True. Except, that's called javascript -- not ajax.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.
Last edited by kyberfabrikken on Sat Jun 02, 2007 12:20 pm, edited 1 time in total.