About Me

My photo
Experienced Web Developer using C#, ASP Classic (VBScript) and ASP.NET, MySQL, T-SQL, and other SQL variants, JavaScript (W3Schools Certified and very well versed in jQuery and learning Dojo), and XML. Heavy interest in JavaScript, framework creation on various language platforms, and keeping up with the best industry-accepted practices.

Monday, September 14, 2009

References in JavaScript

So, I've been getting more refined in my usage of JavaScript, and I've stumbled across a functionality that I knew existed, but didn't know that it existed to the extent that it does, and it's really made me kind of happy.

Since JavaScript is standard at my job, I get to use its glory in every application I create!  Woot!

As many people know, one of JavaScripts flaws is that it's a language that is based on global variables.

Douglas Crockford says this is evil, and I have to agree.  It gives a lot of room for libraries and plug-ins to collide with each other and make an awful mess.

In the current portion of the project that I'm working on, I am setting up an administration page to allow people with a certain level of access the ability to completely edit call records in anyway they see fit.

I use jQuery's datepicker (with a modified theme created by theme roller) for the calendar (you should check it out here), allow the user to submit a date range and an employee to pull back call records for and then list the call records out in a nice list for them to select which one they want/need to edit.

They then select the individual call, which is listed by Submit Date and Customer ID.  That brings them to the view/edit page.

I pull back all the call's information via AJAX to an ASP validation page.  My ASP page formats the data in a JSON format, and using Douglas Crockford's json_parse function, I am able to parse the XML into an easy-to-use JavaScript complient JSON object.

Neat.

My initial thought was to create a global variable to house the call record that was currently being edited...icky...and then it hit me.  Maybe I can pass the internal object to all of my functions that edit/update, and maybe--just maybe, it will update the object, keeping all the references current, which would allow me to skate by with no additional global variables!

So, I tested it...and it worked!



I ran editStuff and then updateRecord and the new value stuck!

Just another reason why I love JavaScript.

No comments:

Post a Comment