【问题标题】:Mootools - how to fix variable scope inside initialize and inside a ajax requestMootools - 如何在初始化和ajax请求中修复变量范围
【发布时间】:2011-07-19 07:21:02
【问题描述】:

我无法让我的 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);
    }

}

【问题讨论】:

    标签: javascript ajax mootools request scope


    【解决方案1】:

    试试这个:

       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

    祝你好运

    【讨论】:

    • 非常感谢您的快速回复,绑定就像使用 this.options.maxSlots 所需的魅力一样,但没有发生任何事情:D 但我真的不明白它为什么有效,绑定有什么作用?
    • mootools.net/docs/core/Types/Function#Function:bind 你需要绑定作用域,因为成功函数的作用域和类的作用域不一样。当您使用绑定时,您是在说“在函数内部,使用类'this 而不是函数的this” =P 我很难解释它
    • 非常感谢我现在明白了:D
    • 呃。让我们知道这是否适合您,ajax 是异步的,这意味着 this.maxSlots 上的循环不会按预期工作,数据将在事件稍后到达并且在运行时,循环不会像 this.maxSlots 那样做很多事情0.
    • @Dimitar Christoff 是正确的。您需要将 async:false 添加到您的 Request 对象或将循环放在 onSuccess() 方法中
    猜你喜欢
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 2012-05-10
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    相关资源
    最近更新 更多