Welcome Guest, Not a member yet? Register   Sign In
Ajax and MVC
#4

[eluser]Michael Ekoka[/eluser]
Hi JosGo, I am not aware of any universally accepted methodology to integrate Ajax into MVC, but I think you're on the right path. It is an interesting thread that you're opening, I wish more people would share their view on the matter. Personally, from what I have learned about Ajax application development, I think there are currently 2 or 3 approaches that are often used in projects:

The first one is to consider the application being developed as a desktop application running in a browser (e.g. zimbra), so Javascript and Ajax are an inherent part of the whole. If you don't have them enabled, it is accepted that you won't run the application. The browser is only used as a convenience, not as a way to make things universally accessible. This approach simplifies development a lot and integrates well into MVC, as virtually any request made to the server uses ajax. Your application will usually load one main layout on the first request, and from then on your controllers will only return very specific view fragments for each action.

The second approach, which is actually the first one with an addition, consists of splitting development of the same application in 2 projects. One with ajax and one without (e.g. gmail). The separate teams will work well on their own, but what becomes difficult about this is to keep the 2 projects in synch. For a company with a treasure chest like google, this is trivial.

The last one that I am familiar with and which is what I would adopt if/when I'll decide to "ajaxify" my application, is known as "Progressive Enhancement". It promotes incremental, unobtrusive javascript/ajax additions to already existing basic PHP/HTML actions. The principle behind that method of development is to use javascript, if enabled, to "hijack" normal hyperlinks, and replace their actions with some ajax counterparts. If js is not enabled the action proceeds as originally intended in pure HTML. One of the major benefits of this approach is graceful degradation. Your application should work normally with or without js enabled. If you use this approach you usually start as a normal PHP/HTML app, but you always plan for a later ajax integration. For this, one technique consists of forking your controller's action at the point where it loads a view.

Code:
if($this->request->is_ajax()){
    $this->load->view('ax_my_view',$data);
}else{
    $this->load->view('my_view',$data);
}

or if you rather decide to let your javascript modify the view from the browser, you would return raw data via json, xml or some other scheme:

Code:
if($this->request->is_ajax()){
    $this->json_send($data);
}else{
    $this->load->view('my_view',$data);
}

For more info about progressive enhancement look up the Hijax framework and any combinations of the keywords "unobtrusive javascript ajax progressive enhancement hijack".


Messages In This Thread
Ajax and MVC - by El Forum - 07-11-2007, 12:45 PM
Ajax and MVC - by El Forum - 07-11-2007, 02:34 PM
Ajax and MVC - by El Forum - 07-11-2007, 03:28 PM
Ajax and MVC - by El Forum - 07-12-2007, 10:28 AM
Ajax and MVC - by El Forum - 07-12-2007, 08:39 PM
Ajax and MVC - by El Forum - 07-12-2007, 10:43 PM
Ajax and MVC - by El Forum - 07-13-2007, 01:10 PM
Ajax and MVC - by El Forum - 07-13-2007, 01:25 PM



Theme © iAndrew 2016 - Forum software by © MyBB