Hüseyin Okumuş Hüseyin Okumuş - 5 months ago 12
jQuery Question

Jquery object "undefined" firstly call

I have a classes that,

var rowJson;
$.row = {

getRow: function (rowId) {
$.ajax({
type: "POST",
url: "bla bla post link",
data: "{bla bala data}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (dc, status) { rowJson = JSON.parse(dc.d);},
error: function (request, status) { rowJson = "-1"; alert(request.responseText) }
});
},
success: function () { if (rowJson == "-1") { return false; } else { return true; } },
Id: function () { return rowJson[0].Id;},
Uygulama: function () { return rowJson[0].Uygulama;},
Kullanici: function () { return rowJson[0].Kullanici;},
Seviye: function () { return rowJson[0].Seviye;},
Durum: function () { return rowJson[0].Durum;},
Baslik: function () { return rowJson[0].Baslik;},
TarihSaat: function () { return rowJson[0].TarihSaat;},
Aciklama: function () { return rowJson[0].Aciklama;},
TalepDurumId: function () { return rowJson[0].TalepDurumId;},
UygulamaId: function () { return rowJson[0].UygulamaId;},
TalepSeviyeId: function () { return rowJson[0].TalepSeviyeId;},
}


after I called like this way,

function _rowInfo(rowId) {
pWaitShow("Please Wait");
$.row.getRow(rowId);
if($.row.success())
{
pWaitHide();
console.log([kbJson,$.row.rowJson])
}
else
{
pWaitHide();
$.ms.standartError();
}


}


console value is "undefined"when i called this function (_rowInfo(5);) firstly
but console value is real object when i called this function (_rowInfo(5);) after that firstly called I mean say secondly,thirstly

Answer

The response comes from an asynchronous call, so you should do something like this to handle this behavior :

You can change the getRow function like that :

 getRow: function(rowId, callback) {   
   $.ajax({
     type: "POST",
     url: "bla bla post link",
     data: "{bla bala data}",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function(dc, status) {
       rowJson = JSON.parse(dc.d);
       callback(rowJson);
     },
     error: function(request, status) {
       rowJson = "-1";
       alert(request.responseText)
       callback(rowJson);
     }
   });
 },

And change your _rowInfo to :

 function _rowInfo(rowId) {
   pWaitShow("Please Wait");
   $.row.getRow(rowId, function(row) {
     if($.row.success())
       pWaitHide();
       console.log([kbJson, row])
     } else {
       pWaitHide();
       $.ms.standartError();
     }
   });
 }