I work in a lot of ASP.NET webforms projects. Since im not a big fan of postbacks, i tend to write a lot of jquery ajax calls to handle the cilent -> server calls.
But keeping most of the logic in script tags / javascript files usually gets messy pretty fast, even though im trying to use a lot of functions and also simulating classes. I should add that 99% of the code is jquery, not “pure” javascript.
So i would like to know how you guys deal with this issue. I cant be the only one 🙂 Thanks in advance
I really like the “unobtrusive javascript” approach, where you load pretty much all your static javascript/jQuery code in a bundled, minified, cached javascript file, and then you use class and “data-” annotations on your HTML elements to flag them for certain actions. The javascript is written to scan your DOM for these flags and set up its logical behavior accordingly.
This approach helps to force you to organize your javascript into logical modules, which tends to help reduce repetitive code. It separates the javascript from the HTML.
The only thing to be careful of is that dynamically-loaded content also needs to be scanned, so any time you add AJAX-provided content to the DOM, be sure you invoke your scanning method on the added elements.