Mootools - 如何修复初始化和 ajax 请求内的变量范围
我无法让 for 循环读取请求之外的 maxSlots 变量。我不知道如何或在哪里声明变量以使其对于每个类都是本地的,但对于洞类来说是全局的。
//INITIALIZATION
initialize: function(){
maxSlots = 0;
var myRequest = new Request({
url: 'getInventory.php',
method: 'post',
onSuccess: function(responseText){
maxSlots = responseText;
console.log(maxSlots);
},
onSFailure: function(){
alert('noo');
}
});
myRequest.send();
for (var i = 1; i <= maxSlots; i++){
var slot = new Element('div', {id: 'slot'+i, class: 'slot'});
slot.inject(invContainer);
}
}
编辑: 好吧,我尝试将变量更改为选项,请求内的警报= 12,但如果我在请求后发出警报,它会说未定义......仍然是同样的问题。
//VARIABLES
options: {
id: '',
size: '',
maxSlots: '2'
},
//INITIALIZATION
initialize: function(options){
this.setOptions(options);
var myRequest = new Request({
url: 'getInventory.php',
method: 'post',
onSuccess: function(responseText){
this.maxSlots = responseText;
alert(this.maxSlots)
},
onSFailure: function(){
alert('noo');
}
});
myRequest.send();
alert(this.maxSlots)
for (var i = 1; i <= this.maxSlots; i++){
var slot = new Element('div', {id: 'slot'+i, class: 'slot'});
slot.inject(invContainer);
}
}
I cant get my for loop to read the maxSlots variable outside of the request. I dont know how or where to declare the variable to make it local to each class but global for the hole class.
//INITIALIZATION
initialize: function(){
maxSlots = 0;
var myRequest = new Request({
url: 'getInventory.php',
method: 'post',
onSuccess: function(responseText){
maxSlots = responseText;
console.log(maxSlots);
},
onSFailure: function(){
alert('noo');
}
});
myRequest.send();
for (var i = 1; i <= maxSlots; i++){
var slot = new Element('div', {id: 'slot'+i, class: 'slot'});
slot.inject(invContainer);
}
}
EDIT:
Ok I tried changing the variable into an option, the alert inside the request = 12 but if i make an alert after the request it says undefined... still the same problem.
//VARIABLES
options: {
id: '',
size: '',
maxSlots: '2'
},
//INITIALIZATION
initialize: function(options){
this.setOptions(options);
var myRequest = new Request({
url: 'getInventory.php',
method: 'post',
onSuccess: function(responseText){
this.maxSlots = responseText;
alert(this.maxSlots)
},
onSFailure: function(){
alert('noo');
}
});
myRequest.send();
alert(this.maxSlots)
for (var i = 1; i <= this.maxSlots; i++){
var slot = new Element('div', {id: 'slot'+i, class: 'slot'});
slot.inject(invContainer);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
你也可以将变量保存在
this.options
祝你好运
try this:
you could also save the variable in the
this.options
Good Luck