I had learned that following piece of code is bad because it is obstrusive javas
ID: 646725 • Letter: I
Question
I had learned that following piece of code is bad because it is obstrusive javascript and mixes presentation with logic:
<a href="">archive</a>
The best practice was to add some sort of identifier to the link element and then bound an event to it on a separate file. Something among those lines
$("#archive").click(function() {
//do stuff
});
See: Why is using onclick() in html a bad practice
How come now most Javascripts MVC frameworks like Backbone.js, Angular.js, etc now encourage pieces of code like this:
<a href="" ng-click="archive()">archive</a>
Taken from: http://angularjs.org/#todo-html
Why is this now ok?
Explanation / Answer
To be honest, I consider it ugly because of the () which look too much like eval'd code.
However, the reason why it's still "clean" is that you are not dealing with a global function or are able/encouraged to put arbitrary code in that attribute. Instead you specify that the element will use the method with the given name in the controller.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.