Sponsored by Gluten Free Waffles and Sliced Bananas

Friday, March 22, 2013

Event Namespacing with jQuery

2:21 PM Posted by Tyson Nero No comments
It's a standard practice to bind and unbind events using general event types such as "click". You would probably do something like this.


However, there are cases, especially with third party libraries for instance, where event collisions could occur. Let's say you bring in a plugin that does some type of magic on a button using click events. You too want to do your own magic and also bind to the click event. Time passes, and for some reason, the plugin gets destroyed and unbinds the click event. Not only has it cleaned up itself, but your event handlers are gone too!

The solution is event namespacing, which is simply adding class names to your events:


Event namespacing comes in extra handy when writing your own plugins. Remember, there are many other plugins out there, and just like writing C# code, you want to define unique namespaces that will hopefully not collide with other libraries.

Organization Just Doesn't End With Code

10:49 AM Posted by Tyson Nero No comments

This morning, as I reached into my dishwasher to put some more items in, I noticed a broken glass. It seems all the items on the top shelf weren't loaded in an organized manner, and a coffee mug handle had been pushed into a glass when an unnamed person tried to overload the area.

Well, I proceeded to remove the broken glass and reorganize the top shelf to avoid any other catastrophes of such magnitude. Then, I noticed my finger was bleeding and the situation was brought to a whole other level.

Several minutes later, my unnamed spouse entered the room to notice the broken glass, a reorganized dishwasher, and some bloodshed on a few other items in the dishwasher. With a quick glance she turned to me and said:

“Maybe if you hadn't reorganized the dishwasher, you wouldn't have got blood on everything.”

Monday, March 18, 2013

Simple Reverse Iterations in C#

11:05 AM Posted by Tyson Nero No comments
There are some situations that require the use of iterating in reverse. It's not such a hard concept to iterate backwards, however, there are cases where using the syntax of a for loop isn't the most satisfactory method. Thankfully, in .NET 3.5 the Reverse() extension method was added, which allows you to simply use a for each in cases where you want minimal code pollution such as an ASP.NET MVC view.

Let's say you have this simple array:

 string[] names = { "Barry", "Bob", "Bill" };  

Here is an example of using a for loop to do a traditional reverse iteration:

 for (int i = names.Length - 1; i >= 0; i--)  
 {  
   Console.WriteLine(names[i]);  
 }  

Now, here's an example of using the .NET 3.5 Linq extension:

 foreach (var name in names.Reverse())  
 {  
   Console.WriteLine(name);  
 }  

Note: Using the Reverse() method is actually slower. So, use with caution when speed at the lowest level is not a factor.