Friday, August 5, 2011

jQuery toggleClass with Visibility

I'm not sure why jQuery uses the CSS property "display" to toggle hide and show, since "display" when set to "none" (which jQuery.hide() does) removes the space it takes up, and if you have elements adjacent, they're going to move around.

The workaround is to use toggleClass with a class you create which is used exclusively for setting "visibility" to "hidden" (instead of "visible"). When an element is "hidden", it is still there taking up space, keeping other elements properly aligned, it just is invisible.

The typical example of the use case is when you have a blank area that when hovered over displays some tool icons, or a delete X.

However, you might run into a confusing problem if you try this without giving the element to be toggled on and off the invisible class to begin with.
Ex.:

//style
.hidden{
visibility: hidden;
}
//HTML
<span class='hidden del'>X</span>
......
//jQuery
$('.editbay').hover(function(){
$(this).find('span').toggleClass('hidden');
});

Friday, July 29, 2011

Firebug musts

for(x in console)
{
console.log(console[x].name)
};

console.dir(console);

Thursday, April 7, 2011

Lambda Expressions and Delegates

"[T]he syntax for delegates is a little confusing and verbose. Lambda expressions offer a much more terse syntax for defining an anonymous method. " - Scott Mitchell

Following excerpt from http://www.codeguru.com/vb/gen/vb_general/ideincludingvisualstudionet/article.php/c15441/Slow-Chat-with-the-MS-Visual-Basic-Team.htm#page-2



Question: (Originally by techgnome)
As I understand it, Lambda Expressions were added to provide functionality for LINQ. My questions are as follows:
  1. What the heck is it?
  2. What would I use it for? Bonus points for explaining why I would and/or not use it in some circumstances. Or better, a situation in which I could use it, but shouldn't.
  3. Could you provide an example of one? This is where I've had problems; most of the examples I've seen have been so esoteric, that I just don't get it.
MS Response: (funkyonex)
Hi technome,
I actually had trouble learning them as well and am very thankful that Visual Basic provides the common ones you use in LINQ queries as query expressions instead. So, I think I'll go for the "bonus points"...
Here's an example of a simple Lambda Expression, or as I like to call them, inline functions, that adds x to itself:
  1. Dim myFunc = Function(x) x + x
To call this, we could write:
  1. Console.Writeline(myFunc(2))
Which results in: 4
Extension Methods are another new feature and allow you to create methods that extend any type. For instance, you could create your own Print method on the string class. The LINQ framework relies heavily on Lambda expressions as well as Extension Methods. In fact, the LINQ syntax we see in Visual Basic is really just abstracting away Extension Methods and Lambda Expressions for us in most cases. For instance, the Sum extension method accepts a Lambda Expression as one of its parameters. Say we want to sum only even numbers in an array. We can do this in one line of code using a Lambda Expression:
  1. Dim x As Integer() = {1, 2, 3, 4, 5, 6}
  2. Dim sumofEvens = x.Sum(Function(y) If(y Mod 2 = 0, y, 0))
Lambda functions start to get really useful (and necessary) when we start writing LINQ queries. Say we want to Sum all of the products in our products collection by multiplying the UnitPrice by the UnitsInStock. We could write this:
  1. Dim total = (From Product In db.Products) _
  2. .Sum(Function(p As Product) p.UnitPrice * p.UnitsInStock)
But why? (I think this is my bonus point) In Visual Basic, we don't have to write Lambda Expressions to do aggregate operations like this. (See here: http://msdn2.microsoft.com/en-us/library/bb546138(VS.90).aspx.) We can instead write this using Visual Basic's Query Expressions:
  1. Dim total = Aggregate Product In db.Products _
  2. Into Sum(Product.UnitPrice * Product.UnitsInStock)
I love it! Much easier to figure out what's going on, IMHO. I hope that helps clear it up.