Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 988
Nyxynyx Avatar asked Jun 23 '11 04:06

Nyxynyx


People also ask

Can we implement AJAX using jQuery in MVC?

Using AJAX In ASP.NET MVC. Implementation of Ajax can be done in two way in ASP.Net Application: using Update Panel and, using jQuery.

Can we use AJAX in MVC?

Web. Mvc. Ajax namespaces can be combined with JavaScript and MVC partial views to create flexible interactive web pages with minimal code. When using these resources, developers should be aware of a few techniques necessary to create effective code.


1 Answers

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.

like image 155
Daveo Avatar answered Oct 06 '22 01:10

Daveo