JavaScript,MooTools和变量/对象范围

时间:2010-01-07 21:48:11

标签: javascript events scope mootools draggable

我可能在这里使用错误的模式,但如果我是,我希望有人可以纠正我!

我有一个表示用户工作空间的对象,并且响应服务器操作,它克隆div,更改其某些属性,将其放置在工作区上,使其可拖动并添加onComplete操作。此操作旨在调用workspace对象的另一个方法,将div的更新位置发送回服务器以进行布局持久性。

我已经将课程剥离了相关的方法:

var MooPanel = new Class( {
    createDiv: function() {
        var newDiv = existingDiv.clone();
        var dragHandle = newDiv.title;

        var move = new Drag(newDiv, {
            'handle':  dragHandle,
            'x': true,
            'y': true,
           /* original line: 
            'onComplete': function( el, dr ) { updateCoordinates( el, dr ); }
            */
           /* working line: */
            'onComplete': function( el, dr ) { this.updateCoords(el); }.bind(this)
        }
    },
    updateCoordinates: function( el, dr ) {
        send.to.server();
    }
});

现在,我无法调用我的updateCoordinates方法。我尝试了this和其他关键字的各种组合,但不能完全正确。

拨打updateCoordinates的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

试试这个(双关语):

onComplete: function( el, dr ) { this.updateCoordinates( el, dr ) }.bind(this);
你的onComplete函数this中的

指的是onComplete函数本身。通过将函数绑定到类this,然后引用类实例本身。

相关问题