Search code examples
javascriptmodel-view-controllerseparation-of-concerns

Does data-id violate separation of concerns


My question is simple:

Does adding the "data-" notation to your mark-up violate MVC's separation of concerns?

Example: <div id="myDiv" data-id="34"></div>

or if you're using a templating system, then <div id="myDiv" data-id="{{myPrimaryKey}}"></div>

There's some debate about that amongst my co-workers. My opinion is that it does violate SoC because you are embedding data into your HTML mark-up (AKA your view). It seems like anything within your markup should only be concerned with the actual displaying/formatting data. But maybe I'm being too much of a purist?

Background: The project that trigged this discussion is a pure JavaScript client with RESTful web services as the back end. Data is retrieved via AJAX calls to the web service.


Solution

  • You're being too much of a purist. This is what the data- stuff was made for. Sometimes you need to annotate nodes with user data so that you can make URLs and stuff to query the server or for JavaScript's benefit.