Mootools - 如何修复初始化和 ajax 请求内的变量范围

发布于 2024-11-25 06:18:14 字数 1509 浏览 1 评论 0原文

我无法让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慈悲佛祖 2024-12-02 06:18:14

试试这个:

   initialize: function(){

        this.maxSlots = 0;
        var myRequest = new Request({

            url: 'getInventory.php',
            method: 'post',     
            onSuccess: function(responseText){

                this.maxSlots = responseText;
                console.log(this.maxSlots);
            }.bind(this),  // <-- always bind the scope in functions

            onSFailure: function(){
                alert('noo');
            }       

    });
    myRequest.send();

    for (var i = 1; i <= this.maxSlots; i++){

        var slot = new Element('div', {id: 'slot'+i, class: 'slot'});
        slot.inject(invContainer);
    }


}

你也可以将变量保存在 this.options

祝你好运

try this:

   initialize: function(){

        this.maxSlots = 0;
        var myRequest = new Request({

            url: 'getInventory.php',
            method: 'post',     
            onSuccess: function(responseText){

                this.maxSlots = responseText;
                console.log(this.maxSlots);
            }.bind(this),  // <-- always bind the scope in functions

            onSFailure: function(){
                alert('noo');
            }       

    });
    myRequest.send();

    for (var i = 1; i <= this.maxSlots; i++){

        var slot = new Element('div', {id: 'slot'+i, class: 'slot'});
        slot.inject(invContainer);
    }


}

you could also save the variable in the this.options

Good Luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文