Getting started with bindings – part VI

Today we’ll be wrapping up this topic with a short post on the ignoreErrors property. When you use bindings and decide to use conversion functions (ie, you set the convert or convertBack properties), you might end up getting exceptions. To illustrate this behavior, we’ll be forcing then in the previous example:

function updateTotal(val, binding) {
    if( !(/^d+$/.test(val)) ){
        var err = Error.create("units must be bigger than 0", null);
        throw err;
    }
    var price = $get("unitPrice").value;
    var indexOf$ = price.indexOf(''$'') + 1;
    var correctVal = parseFloat( price.substr(indexOf$) );
    var numUnits = parseInt(val);
    return "$" + (correctVal * numUnits);
}

As you can see,  updateTotal throws an error when the value you specify in the units textbox isn’t an integer (btw, we’re relying in a regular  expression for achieving that verification). Let’s see what happens when we try to pass a non-integer value:

errorBinding

By default, exceptions aren’t trapped and will propagate until someone handles them. You can change this default behavior by setting the ignoreErrors property to true:

Total: <input type="text" id="total"
     readonly="readonly"
     sys:value="{binding value, source=units, convert=updateTotal, ignoreErrors=true}" />  

If you re-run the sample,you’ll see that you’ll end up with the “wrong result” (ie,$0.2 for the total price) instead of getting an exception.  When you set the ignoreErrors property to true, you’re saying that the conversion function execution should be wrapped in a try/catch block which will swallow all the exceptions generated during its execution.

And that’s it. As you can see, bindings aren’t that complicated. However, they’re really useful as we’ll see in future posts when we start using them with other features introduced by the MS AJAX library.

Advertisements

~ by Luis Abreu on October 12, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: