Ideal Code, I can't today
Austin French
Posted on September 1, 2021
The real world, is rarely ideal
Today, I had to hack it. And I don't mean hammer on the keyboard to get a feature done. Nor do I mean "debug it until I make it", I mean, work against everything I think good, ideal code is. I mean, make something that I feel like it shouldn't be in a code base, part of a library until legacy code is updated.
Backstory
We have an angularJS application, running with jQuery, MVC, and various libraries written for that stack. I've been architecting for months now a path to Angular using ngUpgrade. Most of it has been going great:
- Make a thing
- Set up the components, services, and classes to make an Angular native and well designed pattern.
- Do a little hackery with window variables from the MVC views.
- Looks good, functions well, and doesn't break anything the Angular creators would warn against in a style guide.
Hacking it
This though, isn't something I ever thought would be a thing.
Todays goal:
- Create a parent Angular component
- Define some meta data to pass to a directive component
- Have the directive convert the parent components meta data to HTML
- Insert the HTML into a rendered element from a library
- Have the child component attach the parent component's functions to the window as a function for the onclick attribute of the library rendered angularJs / HTML to call.
It worked!
And it works!
I can create an instance of a class, and assign it something like:
myFunction(myArgument: type){
// the stuff we need to do in the parent component
// from the 3rd party angularJs/ Jquery library inserted to the
// DOM in a child component rendered HTML
}
element.Function = this.myCallbackFunction;
I am both stoked that I was able to get the basic functionality, but also heartbroken that I had to write something this janky.
Conclusion
The real world is rarely as ideal as the code standard we push. I don't know how long I will have to support this code, but until the called library is fixed / modernized we are stuck. I pity all of our developers just learning Angular, and who might have to grok this concept. And I especially fear for those supporting it years or a decade down the road if the library isn't ever modernized properly.
Posted on September 1, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.