08 April, 2013

Global Error Handling through javascript

In java script we can handle all  uncaught errors on a single place which is handy to manage exception handling in application.

There are try catch block to handle block level exception but there are so many which left and thrown run time. Following code can do the trick for this.


  window.addEventListener("error", function (evt) {  
    log.error(evt.message   
             + "\n File: "   
             + parseURL(evt.filename).attr["file"]   
             + "\n Line No: "  
             + evt.lineno);  
    console.log(evt);  
  });  

parseURL() is UFD used to extract filename from path.

log.error() is a UFD used to log error in file.

these two function can be written according to your need.

10 October, 2012

jQuery problem with simultaneous events

I was working on some jQuery validation logic in my project when I came across this problem. When two continuous events fired from jQuery or jQuery mobile i.e. focus () and select () or blur () and click () only one event get fired and other one not. This is very annoying problem which sometime force you to scratch your head for hours and days. I was also having same problem in one of my module where blur and click are called sequentially. I am not sure that this also happen with the traditional java script or not.

Scenario



I have two controls a textbox and a button A textbox has blur event and a button has click event. Now if you write something in textbox and click the button directly only blur event get fired but click event don’t. The ideal condition should be that first blur should called and then click. so now if your form has to get post on button click or some other logic written on its click

<input id="text1" type="text" />
<input id="btn" type="button" value="click" />
$("#btn").click(function() {
    alert("click")
})
$("#text1").blur(function() {
    alert("blur")
})

Solution



Well you can say that it is not a proper solution but just a fix to achieve what is expected but this is the best what came out after hours of efforts and brain exercise. We have to capture the condition that button has been clicked, before blur event get fired. Mousedown is the best event we can use for this. so need to take a variable to set it true on button mousedown and in blur event we can check if the variable is true or not i.e. the button is also clicked or it is just a simple blur triggered in result of lost focus from text box. And there we can explicitly call the button click.

var clickwithblur = false;

$("#btn").click(function() {
    alert("click")
}).mousedown(function() {
    clickwithblur = true;
})
$("#text1").blur(function() {
    alert("blur")
    if (clickwithblur) {
        $("#btn").click()
    }
})​

Working Solution



Fiddle Link


http://jsfiddle.net/amitrdx/TmPtZ

23 July, 2012

Enum types, FlagAttribute & Zero value

Enum types, FlagAttribute & Zero value:
We all know about Enums types and use them every single day. What is not that often used is to decorate the Enum type with the FlagsAttribute.
When an Enum type has the FlagsAttribute we can assign multiple values to it and thus combine multiple information into a single enum.
The enum values should be a power of two so that a bit set is achieved.
Here is a typical Enum type:
public enum OperationMode
{
    /// <summary>
    /// No operation mode
    /// </summary>
    None = 0,
    /// <summary>
    /// Standard operation mode
    /// </summary>
    Standard = 1,
    /// <summary>
    /// Accept bubble requests mode
    /// </summary>
    Parent = 2
}

In such scenario no values combination are possible. In the following scenario a default operation mode exists and combination is used:

[Flags]
public enum OperationMode
{
    /// <summary>
    /// Asynchronous operation mode
    /// </summary>
    Async = 0,
    /// <summary>
    /// Synchronous operation mode
    /// </summary>
    Sync = 1,
    /// <summary>
    /// Accept bubble requests mode
    /// </summary>
    Parent = 2
}

Now, it’s possible to do statements like:

[DefaultValue(OperationMode.Async)]
[TypeConverter(typeof(EnumConverter))]
public OperationMode Mode { get; set; }

/// <summary>
/// Gets a value indicating whether this instance supports request from childrens.
/// </summary>
public bool IsParent
{
    get { return (this.Mode & OperationMode.Parent) == OperationMode.Parent; }
}

or

switch (this.Mode)
{
    case OperationMode.Sync | OperationMode.Parent:
        Console.WriteLine("Sync,Parent");
        break;
[…]

But there is something that you should never forget: Zero is the absorber element for the bitwise AND operation.

So, checking for OperationMode.Async (the Zero value) mode just like the OperationMode.Parent mode makes no sense since it will always be true:

(this.Mode & 0x0) == 0x0

Instead, inverse logic should be used: OperationMode.Async = !OperationMode.Sync

public bool IsAsync
{
    get { return (this.Mode & ContentManagerOperationMode.Sync) != ContentManagerOperationMode.Sync; }
}

or

public bool IsAsync
{
    get { return (int)this.Mode == 0; }
}
The above samples snippets were taken from an ASP.NET control and enabled the following markup usage:

<my:Control runat="server" Mode="Sync,Parent">

Drawback
Zero value is the absorber element for the bitwise AND operation

Be very carefully when evaluating the Zero value, either evaluate the enum value as an integer or use inverse logic.