开发者

How to carry "getjson" functionality from one func to another?

开发者 https://www.devze.com 2023-01-09 00:54 出处:网络
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.

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
    });
    $.g开发者_如何转开发etJSON(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?


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);
 }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号