Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ES6+;Jquery:如何从ajax请求的成功回调函数中调用类的方法?_Javascript_Jquery_Ecmascript 6 - Fatal编程技术网

Javascript ES6+;Jquery:如何从ajax请求的成功回调函数中调用类的方法?

Javascript ES6+;Jquery:如何从ajax请求的成功回调函数中调用类的方法?,javascript,jquery,ecmascript-6,Javascript,Jquery,Ecmascript 6,我正在使用新的标准ES6创建一个网格,使用类可以在将来扩展功能,并使用jquery方法$.ajax。在我开始出现范围问题之前,一切都很顺利。我想从ajax调用中调用该类的一个方法。我该怎么做?。我尝试使用this.method(),method()…但是运气不好…..我还尝试在构造函数方法中绑定该方法 this.generateGridHeader=this.generateGridHeader.bind(this); 也没有任何运气 这是代码,问题在于成功回调函数中的buildGrid()方

我正在使用新的标准ES6创建一个网格,使用类可以在将来扩展功能,并使用jquery方法$.ajax。在我开始出现范围问题之前,一切都很顺利。我想从ajax调用中调用该类的一个方法。我该怎么做?。我尝试使用
this.method()
method()
…但是运气不好…..我还尝试在构造函数方法中绑定该方法

this.generateGridHeader=this.generateGridHeader.bind(this);
也没有任何运气

这是代码,问题在于成功回调函数中的buildGrid()方法,对generateGridHeader()和generateGridBody()的调用……它们没有被识别……我知道它找不到它们,因为这不是正确的范围,但是我不知道如何从里面引用类方法

类网格{

constructor(gridOptions) {
 this.grid_header='';
 this.grid_body='';
 this.grid='';   
 this.options=gridOptions;
 this.cacheDom();
 //this.bindEvents();
 this.buildGrid();
 //I need to bind them cause I'm going to use them inside a callback function
 //this.generateGridHeader=this.generateGridHeader.bind(this);
 //this.generateGridBody=this.generateGridBody.bind(this);
}

cacheDom() {
   this.$greeder = $(this.options.selector);
} 

bindEvents() {

}

buildGrid(){

 if(this.options.parameters !== undefined){
    //it's an ajax call
 $.ajax({
        method: "POST",
        url: this.options.data,
        data: $.param(this.options.parameters),
        success: function(data) {

            this.generateGridHeader();

             this.generateGridBody(data);

            // generateGrid(data, options); 
           // styleGrid(options);  //it can be default with bootstrap if the property style is undefined, if the user wants to put his own style he can use style:'custom' parameter
           // generatePager(options);
           // renderGrid(options);

        }
    });

 }else{
    //it's fixed data from an array or object
    //if typeof == Array parse array
    //if typeof == Object parse object
 }

}

  generateGridHeader(){

    var fields=this.options.fields;
    var header;

    header='<thead><tr>';

     $.each(fields, function(i, item) {
        header+='<th>'+fields[i].title+'</th>';
      });

     header+='</tr></thead>';
     alert(header);

    this.header=header;
}

generateGridBody(data){

} 

styleGrid(){

}    

renderGrid() {

}

context:this
添加到
$.ajax()
设置中。该(context)对象将成为此后所有与ajax相关的回调的上下文

$.ajax({
    method: "POST",
    url: this.options.data,
    context: this,
    ...
现在在
success()
回调函数中,
this
调用
this.generateGridHeader();
this.generateGridBody(数据);
将按预期工作,因为它包含正确的对象(上下文)


有关更多详细信息,请参见。

我建议尝试绑定success函数。即
success:function(){…}.bind(this)
就在
$.ajax()之前。您可以创建一个新的var
var\u,它=this;
然后在
success()中使用
\u that.generateGridHeader()
\u that.generateGridBody(data)
函数。或者您可以将
上下文添加到
$.ajax()中
properties。多亏了这两个。我最终使用了Marcus建议的
上下文:这个
选项。我不知道这个选项。请将其作为答案发布,以便我可以将其标记为解决方案……再次感谢!我已将其添加为下面的答案。很高兴我能提供帮助。您不必绑定
generateGridHeader
generateGridBody
方法,您是从回调调用的,而不是从回调本身调用的。顺便说一句,因为您是在询问ES6,所以只需使用arrow函数进行回调。
$.ajax({
    method: "POST",
    url: this.options.data,
    context: this,
    ...