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
.
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.
ajax function is deprecated.
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.
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
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With