最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

model view controller - Where does JavaScript sit in the MVC pattern of a web application? - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Apr 30, 2011 at 10:44 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Apr 18, 2011 at 15:40 screenm0nkeyscreenm0nkey 18.8k18 gold badges62 silver badges75 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 6

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.

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.

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.

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.

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.

发布评论

评论列表(0)

  1. 暂无评论