Nyxynyx
Nyxynyx

Reputation: 63619

AJAX and jQuery with MVC

How do you organize your controllers, methods, views when you use a MVC model with jQuery with lots of AJAX bits?

Question 1

Do you have a separate controller just for AJAX calls, or do you mix the AJAX methods together with your usual non-AJAX methods in one single controller?

Question 2

If you were to mix AJAX and non-AJAX methods in a single controller, do you have separate AJAX and non-AJAX methods, or do you combine them together (if possible) and pass in a value (NULL or AJAX) which determines whether a normal view or a AJAX view is passed back to the browser.

Question 3

If you have 50 different AJAX calls, and each call requires a method, which in turn requires a view, we end up with a controller with 50 methods and 50 views. Is this good MVC practice? I can think of all AJAX methods in the controller sharing a single view, where the view file contains case conditional statements and the view file is passed a parameter which determines which of the 50 cases will be used. Kind of like compressing 50 views into 1.

Question 4

Instead of having so many views (50 views), what do you think of echoing the output in in the controller method rather than in the view? This way we won't have so many views.

BTW, I'm using CodeIgniter PHP framework for my MVC model

Upvotes: 14

Views: 2124

Answers (3)

Daveo
Daveo

Reputation: 19872

Question 1

I mix the ajax and non-ajax code in to the same controller. That way your code is in a common place easy to find.

Question 2

I combine ajax and non-ajax method together. Makes it easier to use javascript Progressive Enhancement so that people without javascript will still post to the same controller

Question 3

You should not have 1 controller with 50 methods. You should have a controller per piece of functionality. So a User Controller, a Foo controller, a Bar controller - so you may end up with 10 contollers with 5 methods each. This way methods belong in classes specific to their function. I have seperate views and not one big view. You should NOT use LOGIC inside views to determine what is shown, this is the job of the controller. But some controller/methods can return the same view as other methods

Question 4

Or controller should NEVER output HTML. Use views for this that is the whole pupose of MVC to seperate out Code (controllers from) Views (rendering) concerns. Some times my views just return JSON or XML and then I use Javascript templates to update the DOM. Othertimes my views return HTML. For example a Save function on a form. Might just return a Boolean if sucessful. Then my Javascript would hide or show a DIV depending on the response.

Upvotes: 10

Dave Ward
Dave Ward

Reputation: 60580

When you make a jQuery AJAX call, jQuery adds an X-Requested-With HTTP header which you can use to determine whether a given request is an AJAX request or not. With that in mind, it very often makes sense to share the same controller action between AJAX and traditional requests, building a model up and then rendering a view with that model for traditional requests or responding with a JSON serialized representation of the same model for AJAX requests.

Upvotes: 3

isobar
isobar

Reputation: 1175

Question 1 - Separate controller for AJAX. If you are using servlets then you should make use of the new servlet 3.0 support for async support.

http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html?page=2

Question 2 - Since we are using separate controller this is not applicable.

Question 3 - Do you see a scenario where you have 50 unique calls without overlapping functionality? Though you have 50 AJAX calls there can be a lot of reuse at the server side to serve these requests. So 50 calls does not equal to 50 methods. You can wrap each request with a command & execute appropriately instead of an if else which can get out of hand.

Question 4 - 50 calls may not equal to 50 views. You need to reuse here too. You need to separate out your view components & assemble them. Boils down to design.

Upvotes: 3

Related Questions