Avoid Mixing Server Side and Client Side Code
Some
Background
This
article is from a time when I used primarily Asp.net 4 with
razor syntax which was a love and hate relationship. I started out as
a front-end developer but was limited by my peers on the technologies
I could use as well as my skills. Even though I was peer reviewed
much of my code was "acceptable" I however felt it lacked something.
Currently
I
avoid mixing MT (middle-tier) and FE (front-end)
code; it goes in hand with my philosophy on how to design web
applications. I strongly recommend going the extra mile. Ask yourself
how will others use the code; not how will they reuse it, that's
another concern. Also how will you debug it if something goes wrong?
There are many reasons why I avoid doing this. Mainly cause I like to
take the approach of SOC (separation of concerns). After
years of frustration trying to debug finally I made this rule for
myself. Not only with my code but in my process of thought during
developing. Which was be a big task and required some though on a
much bigger picture to work with. However with each application I
built a pattern started stick with me. In the past I watched others use
server code to build out client side code as well as mix the two. Now
days it feels like a complete waste of time to go that route.
Solutions
In
the past I've used hidden inputs that are written from the
back-end which are read from the front end. Then a few years later I
started to use attributes; mostly the custom data attribute that's
available. Now days with my abilities and free time I've gone back.
Looking at it from just a front-end perspective you could write it
all in the front-end. Simple have some of it rendered via server.
However it might seem like I'm going in a circle but
at least then it would be in a single code base (programming
language). So here are my three approaches, I could go more in depth
on the last one but I rather prove it by example in an application.