jQuery – .bind(), .live(), .delegate(), .on() ( and also .unbind(), .die(), .undelegate(), .off() )

Confused, Which one to use, jQuery .bind(), .live(), .delegate(), .on() - Shemeer

The .bind() method registers the type of event and an event handler directly to the DOM element. The .bind() doesn’t work for elements added dynamically that matches the same selector. This method is pretty easy and quick to wire-up event handlers. The shorthand methods like .click(), .hover(), etc make it even easier to wire-up event handlers. There is a performance issue while working with large selection as the method attaches the same event handler to every matched element in the selection.The attachment is done upfront which can have performance issues on page load.

The basic difference between .bind() and .live() is bind will not attach the event handler to those elements which are added/appended after DOM is loaded and there is only one event handler registered instead of the numerous event handlers that could have been registered with the .bind() method. When using .live() events always delegate all the way up to the document. This can affect performance if your DOM tree is deep. Chaining is not properly supported using .live() method. Using event.stopPropagation() is no longer helpful because the event has already delegated all the way up to the document. .live() method is deprecated as of jQuery 1.7 and removed from jQuery 1.9.

The .delegate() method is very powerful, The difference between .live() and .delegate() is, live function can’t be used in chaining. live function needs to be used directly on a selector/element. Also .delegate() works on dynamically added elements to the DOM where the selectors match. Chaining is supported correctly in .delegate().

The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers. Brings uniformity to the various event binding methods. but as the the way we call .on() method the behaviour also changes.

Note: If you are using jQuery 1.7+ then its advised to use .on() for attaching events to elements or selectors.

Read more from CodeProject.

The Solution User Options (.Suo) File in Visual Studio

What is Solution User Options (.suo) file

The solution user options (.suo) file is a structured storage, or compound, file stored in a binary format. This file is used to store user preference settings, and is created automatically when Visual Studio saves a solution.

Location of .suo file

The .suo file will be saved in the folder where your Solution (.sln) file exists. You might wanted to set true for “Show Hidden Files and Folder” in “Folder Options” as .suo file is default by hidden.

What informations are saved in my .suo file

This file is used by Visual Studio to store user/solution specific information such as Opened Files, Expanded Nodes in the Solution Visual Studio .suo fileExplorer, Opened Tool Windows and its Positions, User Tasks, Breakpoints, Start-up Project, Contents of Watch window, Whether the project is loaded/unloaded  etc. The same file is used by Visual Studio Addins (VSPackages) to persist information that are specific to that solution/user.

.suo file and Source Control

Adding user specific information to source control is not a good idea. Most of the code versioning control ignores .suo file by default. If included, the developer, who checks out the latest .suo file, will loose all his personal settings which is stored in the local .suo file.

Visual Studio performance and .suo file

If the size of .suo file is too large then it can affect the performance of Visual Studio. Deleting the .suo file will boost the performance.

Deleting .suo file

Visual Studio creates a new file with the .suo file extension as soon as you open again the solution. But once the file with the .suo file extension is deleted, any of your existing solution user-specific settings will be lost.

App_Offline.htm, an easy way to take asp.net web application offline

You can simply take down an ASP.NET 2.0 or later application offline temporarily by uploading a file called App_Offline.htm in the root of a web application directory.  While the App_Offline.htm file exists, any requests to the Web site are redirected to this file. The file displays a friendly message that tells clients that the Web site is being updated or down due to some reasons.

This is very handy when we do deployments manually. We just need to put App_Offline.htm file in root directory of web application and the ASP.NET runtime will detect the existence of  App_Offline.htm, if it exists, then the ASP.NET runtime will shut-down the application, unload the application domain from the server, and stop processing any new incoming requests for that application.  ASP.NET will also then respond to all requests for dynamic pages in the application by sending back the content of the App_Offline.htm file

When all Web site files have been copied, you can delete the App_offline.htm. Once removed, the next request into the application will cause ASP.NET to load the application and app-domain again, and all things will continue to work as normal.ASP.NET Web Site Application Offline

Most of the automated publishing tools or options also takes advantage of App_Offline.htm

Note: Always make sure that your App_offline.htm page is at least 512 bytes by adding filler tags or hidden text or comments as In Internet Explorer if  “Show Friendly Http Errors”  is set to true, and a server returns a non HTTP-200 status code with less than 512 bytes of content, IE will not show the returned HTML and instead substitutes its own generic status code message. In IE6 “Show Friendly Http Errors” is set to true by default.

SQL Server – Table Hints

Table hints are nothing but a Transact-SQL clause that is specified in the FROM clause of the data manipulation language (DML) statement and affect only the table or view referenced in that clause. Table hints override the default behavior of the query optimizer for the duration of the data manipulation language (DML) statement by specifying a locking method, one or more indexes, a query-processing operation such as a table scan or index seek, or other options.

Because the SQL Server query optimizer typically selects the best execution plan for a query, msdn recommend that hints be used only as a last resort by experienced developers and database administrators.

Microsoft-SQL-Server

Using hints can add a performance penalty and hints that improved performance in earlier editions of SQL Server may not do so in newer versions. However, there are situations where a hint can improve performance or allow queries to execute that would otherwise be blocked

Syntax:-

WITH  ( <table_hint> [ [, ]...n ] )

<table_hint> ::= 
[ NOEXPAND ] { 
    INDEX  ( index_value [ ,...n ] ) | INDEX =  ( index_value )    | FORCESEEK [( index_value ( index_column_name  [ ,... ] ) ) ]
  | FORCESCAN
  | FORCESEEK
  | HOLDLOCK 
  | NOLOCK 
  | NOWAIT
  | PAGLOCK 
  | READCOMMITTED 
  | READCOMMITTEDLOCK 
  | READPAST 
  | READUNCOMMITTED 
  | REPEATABLEREAD 
  | ROWLOCK 
  | SERIALIZABLE 
  | SPATIAL_WINDOW_MAX_CELLS = integer
  | TABLOCK 
  | TABLOCKX 
  | UPDLOCK 
  | XLOCK 
} 

<table_hint_limited> ::=
{
    KEEPIDENTITY 
  | KEEPDEFAULTS 
  | HOLDLOCK 
  | IGNORE_CONSTRAINTS 
  | IGNORE_TRIGGERS 
  | NOLOCK 
  | NOWAIT
  | PAGLOCK 
  | READCOMMITTED 
  | READCOMMITTEDLOCK 
  | READPAST 
  | REPEATABLEREAD 
  | ROWLOCK 
  | SERIALIZABLE 
  | TABLOCK 
  | TABLOCKX 
  | UPDLOCK 
  | XLOCK 
}

Omitting the WITH keyword is a deprecated feature: This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.

Separating hints by spaces rather than commas is a deprecated feature: This feature will be removed in a future version of Microsoft SQL Server. Do not use this feature in new development work, and modify applications that currently use this feature as soon as possible.

Sample:

SELECT * FROM Employees WITH (NOLOCK)

Click here to read more from MSDN

Web Search is now updated to show Quick Search Box

QuickSearchWeb Search is now updated to show Quick Search Box in Visuak Sudio IDE.

and also updated to list asp.net (suggested by Jon Galloway) and pluralsight in the default search list.

Using this Quick Search Box you can easily start searching for anything, from the listed WebSites (including custom). This Quick Search will help those who are frequently searched somthing.

Click here to read or download

The below screen shows the configuration screen of Web Search.

Web Search Config Option

Below screen is how the Web Search plugin looks like,

Search from Visual Studio IDE

WebSearch plugin is now available for Visual Studio 2013

Hello, I just released WebSearch plugin for Visual Studio 2013.

Search from Visual Studio IDE

Web Search plugin supports below options, Read more from WebSearch page

  • Multiple search options included in all Text Editor, Reference Items, Error List. (Integrated search options for Visual Studio IDE)
  • Currently this Add-in works fine with Visual Studio 2005, 2008, 2010, 2012.
  • You can customize web-search by configure option.
  • Open Search in Default Web Browser or Visual Studio Tab or any other installed browser.
  • Change WebSearch context menu position
  • Easy access with Shortcut key. I use Alt+F1.
  • WebSearch context menu availabe in all most all editor files.
  • WebSearch menu also availabe in Tools menu with default search option.
  • Configurable shortcut key.
  • WebSearch menu in Tools menu
  • WebSearch context menu in all Text Editor, Reference Items, Error List.

Using the DebuggerBrowsable Attribute

Visual Studio is very rich with debugging tools and we always use debug windows for troubleshooting issues.

Debug windows gives  us the access to view values of objects and option to edit the same. Lets say I’m working with an object, which is having large number of members in it. Now when I debug I required only few of them and the other member object values are not that much important for me. In this case I will be facing some difficulties to see the required member object in in the debug hierarchy. Which indirectly wasting my time.

To help us in this kind of situation .NET already have Diagnostic Debugger Attributes. In this post we will check the usability of DebuggerBrowsableAttribute.

DebuggerBrowsable Attribute determines if and how a member is displayed in the debugger variable windows. You can add the DebuggerBrowsable attribute to properties, fields and indexers in classes and structures.The constructor for this attribute takes one of the DebuggerBrowsableState enumeration values, which specifies one of the following states:

  • Never indicates that the member is not displayed in the data window. For example, using this value for the DebuggerBrowsableAttribute on a field removes the field from the hierarchy; the field is not displayed when you expand the enclosing type by clicking the plus sign (+) for the type instance.
  • Collapsed indicates that the member is displayed but not expanded by default. This is the default behavior.
  • RootHidden indicates that the member itself is not shown, but its constituent objects are displayed if it is an array or collection.

Let us understand more with a sample example,

The blow code block is used to understand different DebuggerBrowsableState of DebuggerBrowsableAttribute,

static void Main(string[] args)
{
 List employers = new List();
 employers.Add(new Employee
 {
 Id = 1,
 Name = new FullName("Dave", "A"),
 Phone = new List() { "732-751-2921", "732-751-2923" },
 UniqueNumber = System.Guid.NewGuid().ToString()
 });
 employers.Add(new Employee
 {
 Id = 2,
 Name = new FullName("Aydin", "B"),
 Phone = new List() { "732-752-2922", "732-752-2924" },
 UniqueNumber = System.Guid.NewGuid().ToString()
 });
 employers.Add(new Employee
 {
 Id = 3,
 Name = new FullName("John", "C"),
 Phone = new List() { "732-753-2923", "732-752-2932" },
 UniqueNumber = System.Guid.NewGuid().ToString()
 });
 employers.Add(new Employee
 {
 Id = 4,
 Name = new FullName("Peter", "D"),
 Phone = new List() { "732-754-2924", "732-752-3922" },
 UniqueNumber = System.Guid.NewGuid().ToString()
 });
}

Now let me show you the class definition,


class Employee
{
 public int Id { get; set; }

[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
 public FullName Name { get; set; }

[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
 public List Phone { get; set; }

public string UniqueNumber { get; set; }

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
 public string Email { get; set; }

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
 public string Fax { get; set; }

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
 public int Age { get; set; }
}

public class FullName
{
 public FullName(string firstName, string lastName)
 {
 FirstName = firstName;
 LastName = lastName;
 }
 public string FirstName { get; set; }
 public string LastName { get; set; }
}

If you observe the above code you can see that I have decorated properties with DebuggerBrowsable attribute and desired DebuggerBrowsableState. In debug mode, you can check the Employees object to see the effect of this DebuggerBrowsableAttribute.

Below given screen shot shows the debug view of Employee object.Without DebuggerBrowsableAttribute Quick Watch
In the above screenshot we can see that Age, Email, Fax are not listed and FirstName, LastName are shown without root element. Below screenshot shows the Quick watch of employee object without the DebuggerBrowsableAttribute.

DebuggerBrowsableAttribute Quick Watch