When I arrived at my current role, the first and most obvious problem that made maintenance next to impossible was the intertwining of javascript with the C# code behind. It was one of my pet peeves before I arrived to see C# strings used to inject javascript into a page but the codebase here was almost unbelievable - javascript that called a setTimeout with javascript inside it that had multi-escaped double quotes inside the string.
Now, sometimes there's just no avoiding it - if you want to get that truely fantastic maps integration to work really slickly with the data in your working context you might have to inject some dynmically generated javascript. Now, most of the time it would be better to do it by using json, parameterising properly but sometimes it's difficult to avoid. The prime example would be when you're trying to write a server control that outputs javascript appropriate to its environment based on some control parameters.
So, how do we solve the problem? Well, javascript lives in javascript files for a reason. Code seperation is there for a reason too but there just wasn't a mechanism for creating the kind of dynamic javascript we wanted. The solution I came up with was to have the server read in from a series of javascript file resources that contained some placeholder symbols to put variables in to. I did the same for HTML injection where we needed to dynamically output html, which was also pretty useful.
Of course, since we finally managed to convince the boss that jQuery was a good thing the need for all of that has dropped significantly but it's still a nice thing to be able to do. I'll post a link to the javascript reader class and html reader class soon so that anyone that wants to can use it.
No comments:
Post a Comment