4

I was told by a Javascript programmer not to do DOM manipulation in model code.

I also remember hearing a snazzy tune for a JS framework (I forget which one): "Get your models out of the DOM".

This is confusing to me. The DOM is the Document Object Model. To me it sounds like "get your model out of the model".

Should my JS model really not touch the DOM? Can someone set me straight on model v. model?

2
  • 1
    The DOM is the visual representation or visual presentation. Most people would call that the view (what you see and what a user interacts with). It's the Document Object Model to the browser which just means it's the structure that the browser uses to display things, but it's not the data model or object model of your app. Yes, the terminology is a bit unfortunate, but it's not your app's model - that would be something completely different.
    – jfriend00
    Commented Jan 30, 2015 at 5:10
  • stackoverflow.com/questions/27615663/… Commented Jan 30, 2015 at 6:55

2 Answers 2

7

There are a lot of overloaded terms in programming. The "model" in Model-View-Controller has nothing to do with the "model" in Document Object Model.

Arguably the DOM should have been called something like the Document Manipulation Interface, since that's what it actually is: an interface. More importantly, it's an interface for manipulating the content that gets shown directly to the user. In MVC, any code that shows things to the user should be in a View.

It's entirely correct to say that your MVC model should not be manipulating the DOM, for the same reason it shouldn't be generating popups or playing sounds: We want to keep the business entities (models) decoupled from the presentation logic.

1

"Model" in "Document Object Model" is not referring to "Model" in "Model-View-Controller". It's just a Module which we use to manipulate the DOM. We do data handling in Model(MVC) and do some logic in the Controller and what you're referring DOM is to be used in View(MVC) as we are representing the view for the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.