0

anyone can suggest me how to call parse without use parseFunc as argument in aj.call ? I think it's a bad idea "override" prototype.parse and then pass as argument, why cant use the aj.parse directly? ecma5 because it works in Iternet Explorer compatibility mode on (old IE version) Thanks

function AjaxFactory(){

}

AjaxFactory.prototype.async = false;
AjaxFactory.prototype.method = "GET";

AjaxFactory.prototype.parse = function (result){
    return result;
}

AjaxFactory.prototype.call = function (callActionUrl, data, parseFunc){
     jQuery.ajax({
        url: callActionUrl,
            method: this.method,
            data: data,
            success: function (result) {
               return parseFunc.call(this,result);
            },
            async: this.async
        });
}

/*
 * es:*/
var myParser = function (result){
    return "my parser of " + result;  
}

var url = "";
var aj = new AjaxFactory();
aj.parse = myParser;
aj.async =false;
aj.method = "GET";
alert(aj.call("url",{}, aj.parse));

*add 08 Dec 2020

I have also this question: the return value does not return when i do alert(aj.call) so i change as below. But i suppose: does it working when async is true? Maybe 'return outreturn' can be executed before that 'success' was called..can you suggest me? Maybe can i use the result directly in the parse function..right? Thanks

AjaxFactory.prototype.call = function (callActionUrl, data, parseFunc){
   var outreturn;
     jQuery.ajax({
        url: callActionUrl,
            method: this.method,
            data: data,
            success: function (result) {
               outreturn =parseFunc.call(this,result);
            },
            async: this.async
        });
return outreturn;  <-- this can be wrong when async true 
}
robyp7
  • 481
  • 2
  • 7
  • 25
  • Since `AjaxFactory` is a class, you have to refer to it's props as `this.prop`, where `prop` in your case is `parse`. If it still doesn't work, add default values to your `AjaxFactory` function and that will surely handle it as a `class` – kmp Dec 07 '20 at 10:41
  • @kpm you suggest to write "return parse(result)" ? this. not work because i'm in the jQuery object, so i have to set context to this (= the caller). – robyp7 Dec 07 '20 at 14:26
  • I was suggesting to call `this.parse.call(this,result);` Here `this` should refer to `AjaxFactory` and you don't have to pass a callback function – kmp Dec 07 '20 at 14:46
  • "*I think it's a bad idea "override" prototype.parse and then pass as argument*" - it's not really bad, just unnecessary. You can (and should) just do `aj.call("url", {}, myParser);` – Bergi Dec 08 '20 at 09:45
  • "*Maybe 'return outreturn' can be executed before that 'success' was called*" - [Yes](https://stackoverflow.com/q/23667086/1048572)."*Maybe can i use the result directly in the parse function..right?*" - yes, you should. Just put the `alert` call directly in your `myParser` function. Don't have it `return` anything. – Bergi Dec 08 '20 at 09:47

0 Answers0