views:

12

answers:

1

Basically I have a getjson call to call for a bunch load of data. Now I am getting sick of the amount of if data is null else checks.

For Example:

        if (data.Height == "") {
            $('#DataHeight').html('No Data is Available');
        }
        else {
            $('#DataHeight').html(data.Height);
        }

Reason being is the user has to be alerted asap that data is not available.

So I went off and am trying to write a DataCheck.js to include and handle all this outside of my pretty little page :D.

Function:

function DataNullCheck(ObjectName) {
    if (data.ObjectName == "") {
        $('"#Data' + ObjectName + '"').html('No Datablurgh');
    }
    else {
    $('"#Data' + ObjectName + '"').html(data.ObjectName);
    }
}

and to call it inside my function

function getdata() {
    $.ajaxSetup({
        cache: false
    });
    $.getJSON(urldefault, function (data) {
      DataNullCheck('Height');
    .......

And Data is undefined. Because it is not within the JSON call? Any help or hints towards the right direction would be appreciated especially an explanation of why it works one way or another. Thanks in advance.

How would I carry through the getJson call functionality to my little function?

+2  A: 

Pass data as a parameter to your function:

function DataNullCheck(data, ObjectName) { // you can check for null any property of data
                                           // e.g. DataNullcheck(data, "Width"), or
                                           // DataNullCheck(data, "Height") etc.
 if (!data[ObjectName]) {
     $('"#Data' + ObjectName + '"').html('No Datablurgh');
 }
 else {
 $('"#Data' + ObjectName + '"').html(data.ObjectName);
 }
}
naikus
eugh slaps forehead.
Thqr
Thankyou very muchly
Thqr
@Thqr You are welcome
naikus