gpt4 book ai didi

Javascript ES6+Jquery : How do I do to call a method of the class from within a success callback function of an ajax request?

转载 作者:行者123 更新时间:2023-11-30 11:57:47 24 4
gpt4 key购买 nike

<分区>

我正在使用新标准 ES6 使用类制作一个网格,以便能够在未来扩展功能和 jquery 方法 $.ajax。一切都很好,直到我开始遇到范围问题。我想从 ajax 调用中调用该类的方法。我怎么做?。我试过使用 this.method(), method()....但没有运气......我还尝试在构造函数方法中绑定(bind)方法。 ....

this.generateGridHeader=this.generateGridHeader.bind(this);

也没有任何运气......

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

网格类{

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() {

}

/* dataType: 'array', 'json'possible values, if parameters is not defined it's array mode*/
var grid= new Grid({
title:'The grid',
selector:'#grid',
data:'./api/getCustomers.php',
parameters: {"id_customer":"32"},
fields:{
id_customer:{
title:'ID customer'
},
first_name:{
title:'First Name'
},
last_name:{
title:'Last Name'
}
}
});

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com