Tuesday, 13 September 2011

Getting Fiddler to work with localhost or 127.0.0.1

Fiddler, the network package viewer, seems to have a problem viewing local traffic.
As we mostly develop using a local server, this is a major issue.

Fortunately, there is a simple solution, albeit temporary.

Simply add a "." (fullstop) to the end of the host  .

so
127.0.0.1 becomes "127.0.0.1."
localhost becomes "localhost."

Monday, 18 October 2010

Detecting if a JSON object is empty in Javascript

I recently need to verify if an object passed back from the server had data in it, or was empty.
Imagine my surprise when I realised there is no simple way of acheiving this.

Anyway, I wrote a nice simple and fast method to do this.

function isEmptyObject(obj){
        for(var propName in obj){
            if(obj.hasOwnProperty(propName)){
                return false;
            }
        }
        return true;
    }

isObjectEmpty iterates through the properties of an object, checks that the property being checked belongs to the object directly, ie: does not come from a prototype, and returns false.
If there are no properties belonging to the object, then it returns true.

The nice thing, is that it works on Arrays too.

>>> TM.isEmptyObject({})
true
>>> TM.isEmptyObject({1:2})
false
>>> TM.isEmptyObject([])
true
>>> TM.isEmptyObject([1,2])
false


Just be careful, it will fail on any extended object, as the extended function will probably have been put directly into the object itself.

Wednesday, 6 October 2010

Removing spaces from a string in Javascript

Another reminder post for myself really.

If you have a Javascript value that contains spaces or other characters, but want to use the value as an element name or variable name, then you need to remove the bad characters from the string.
This will do it.

"my var with spaces & wierd chars*".replace(/[^\w]/g,"_");

this will output "my_var_with_spaces___wierd_chars_".

Friday, 1 October 2010

Clearing sorting from YUI datatable columns

I have been tearing my hair out (what's left of it) , trying to remove the sorting from a YUI datatable when we call the remote datasource again.

Fortunately, this proved to be really simple once I had spent hours walking through the code paths...

myDataTable.set('sortedBy', null);

Wednesday, 29 September 2010

Changing the YUI Datatable Paginator CurrentPageReport

The CurrentPageReport controls the "(1 of 15)" text shown in the paginator.

This can be easily modified by overriding the template value.

Simply insert this after you have initialised your datatable.

// this goes after datatable is init'd
myDataTable.get("paginator").setAttributeConfig('pageReportTemplate',{
value : '{startIndex} to {endIndex} (of {totalRecords})',
validator : YAHOO.lang.isString
});


Here I have changed it to say "1 to 25 (of 2456)".

ie: it now shows records information rather than page information.

Monday, 27 September 2010

JS Array slicing

Just a reminder to myself about how to change an list into an array in JS.


var args = [].slice.call(arguments, 0);

Monday, 20 September 2010

How to debug in Visual Web Developer 2010 Express

This is pretty simple really.

1. download and install Visual Web Developer 2010 Express.
2. start a new Web Application Project
2a. make sure configuration at the top is set to DEBUG
3. right click on your project in Solutions Explorer (RHS)
3a. Select Properties from the Drop Down
4. Select Web tab
5. Select Start URL in the radio icons, and enter a URL
6. change default browser to IE. (change browse with in VWD)
7. Click Start Debugging (Green play icon)

That should start IE with Visual Studio watching it for bugs.
or if you use a DEBUGGER call in your code.