Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does JavaScript sit in the MVC pattern of a web application?

I'm still confused about where JavaScript code sits in the MVC pattern when building a web application. I thought it worked like this.

  • The Model is the database and the classes required to get the data in/out.
  • The Controller would be the Classes where I write my logic that is, Java servlets, which accept an Ajax request and then make a call to the database;
  • The View is the JSP page which is returned to the Ajax request via the servlet (my Controller)

Because JavaScript code is compiled in the browser I think of it as part of the View, but it's handling user inputs, making server requests based on those events and then returning the data to page, so would that also make in part of the Controller?

Also, what does it mean when they refer to the Domain Model in MVC?

like image 737
screenm0nkey Avatar asked Apr 18 '11 15:04

screenm0nkey


3 Answers

JavaScript is going to be primarily a UI related concern; your view is making an ajax request to the controller. The controller isnt making an ajax request; nor is the model. For all intents and purposes, an ajax request isnt anything different than a normal request; it's just that the browser isnt hanging until your response is returned.

JavaScript also executes in the context of your client, outside the purview of your server, so it should go into the view.

like image 111
Tejs Avatar answered Oct 23 '22 13:10

Tejs


MVC is just a pattern. JavaScript code itself can implement this pattern, so I don't think of it as fitting into some other portion of your server side framework's pattern. Check out Backbone for a good example of using MVC in JavaScript code.

You can model your JavaScript code off of similar concepts that you model your server side code with. The JavaScript code itself will get served up through the view of your server side application, but unless you're only adding eye candy with JavaScript code (which you're not) then the JavaScript code is really its own entity and doesn't necessarily fit into your server side MVC paradigm.

Try to separate the JavaScript code from anything server side. Just consider it an 'add on' that, if disabled in the browser, won't break your application from running. I just add some niceties to allow for better interaction, etc. How you actually model the JavaScript code is up to you, (but I do highly recommend Backbone)

One could also do a Rich frontend in javascript backed only by a data source. In this case, once again, javascript will be responsible for maintaining models, views and controllers.

Domain model generally just refers to the business logic of your application. The brains so to speak of what should actually happen in your app. It's kind of an abstract concept encapsulating all the business logic of an app.

like image 33
brad Avatar answered Oct 23 '22 14:10

brad


Nick, my personal experience in MVC, working with Zend or Spring, I think JavaScript code would be considered as a part of the View since JavaScript code is helping View and is directly interacting with the view. Send and receive of data through Ajax can be considered as a request.

like image 1
Amir s Avatar answered Oct 23 '22 13:10

Amir s