screenm0nkey
screenm0nkey

Reputation: 18795

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.

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?

Upvotes: 13

Views: 2965

Answers (5)

David Houde
David Houde

Reputation: 4778

JavaScript code is part of the View. The view is what gets output to the browser, and while Javascript code doesn't automatically have a visual appearance to it, it can be used to modify the DOM.

When you start talking about Ajax, it's easy to see JavaScript code as something else in the normal flow of things, but you should break down the process of an Ajax request to see it's just another HTTP request.

Some people will have a controller for just Ajax requests, while others may pass an argument to a controller depicting an Ajax request to modify the output.

Either way, JavaScript code sits in the View and you may need to learn some other design strategies regarding Ajax in an MVC setting.

Upvotes: 1

brad
brad

Reputation: 32345

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.

Upvotes: 3

Amir s
Amir s

Reputation: 136

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.

Upvotes: 1

Thomas
Thomas

Reputation: 463

If you use JavaScript to work with the DOM so yes, it is part of the View. But you can still use JavaScript on the server side, in this case it could be part of the code related to the business.

Upvotes: 0

Tejs
Tejs

Reputation: 41236

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.

Upvotes: 6

Related Questions