Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Save AJAX Result in as Class variable

I know that javascript does not use Class, at least not in common sense`. I will like to know how to return and save an AJAX return value in a class variable rather than calling multiple methods within the callback.

var Reader = function(){
  //Initialize some variables
  this.data = null;
}

Reader.prototype.makeAjaxCall = function(urlPath){
   //Make and Ajax call and return some value
   Ajax.success(function(data){
     this.data = data;
   });
}

Reader.prototype.searchData = function(param){
   //Needs access to this.data
}
Reader.prototype.findData = function(id){
  //Needs access to this.data
}

Reader.prototype.deleteItem = function(id){
  // Needs access to this.data
}

In the above code, whatever function that needs access to the data property needs to be called within the ajax success callback, so If I have ten methods that need data, I will have to line all of them up within the callback, which I do not feel is right. How do I minimise the number of functions in the callback and ensure in some other ways that the function is successful and data is saved the instance variable data.

like image 849
James Okpe George Avatar asked Mar 14 '16 11:03

James Okpe George


People also ask

How do I save ajax response to global variable for reuse?

Solution 1 var myResponse; $. ajax({ url: 'PageMethod/GetData', method: 'post', dataType: 'json', data: JSON. stringify({ dataId: "xxx" }), contentType: 'application/json', success: function (data) { myResponse = data.

Is ajax successful deprecated?

ajax function is deprecated.

Is ajax dependent on JavaScript?

Ajax is dependent on Javascript. If there is some Javascript problem with the browser or in the OS, Ajax will not support. Ajax can be problematic in Search engines as it uses Javascript for most of its parts.


2 Answers

In your code:

Reader.prototype.makeAjaxCall = function(urlPath){
   //Make and Ajax call and return some value
   Ajax.success(function(data){
     this.data = data;
   });
}

"this" is not the Reader instance context, you should change it to

Reader.prototype.makeAjaxCall = function(urlPath){
   var myThis = this;
   //Make and Ajax call and return some value
   Ajax.success(function(data){
     myThis .data = data;
   });
}

To keep the pointer to Reader instance in myThis variable and access inside the success function

like image 180
VinhNT Avatar answered Nov 15 '22 16:11

VinhNT


In order to save the return data in a so called Class variable, given your example you need to understand the context where you are using the this keyword. In your example

Reader.prototype.makeAjaxCall = function(urlPath){
   //Make and Ajax call and return some value
   Ajax.success(function(data){
     this.data = data;
   });
}

this is referred to the Ajax object.

Since this.data in Reader object is pretty much encapsulated in the context of the Reader object you need to save it locally before making the Ajax call, in order to be able to access it from withing the call. Like this:

Reader.prototype.makeAjaxCall = function(urlPath){
    var _this = this;
   //Make and Ajax call and return some value
   Ajax.success(function(data){
     _this.data = data;
   });
}

This pretty much solves the context problem and is very helpful when it comes to making function calls. A simple solution is to just define a local variable where you can store the data temporary and then use it to overwrite the the value of this.data Code should look like this:

 Reader.prototype.makeAjaxCall = function(urlPath){
        var tempData;
       //Make and Ajax call and return some value
       Ajax.success(function(data){
         tempData = data;
       });
       this.data = tempData;
    }
like image 45
Vladimir Drenovski Avatar answered Nov 15 '22 17:11

Vladimir Drenovski