调用Google Maps事件侦听器时,Mootools类属性未定义

时间:2010-11-01 16:39:12

标签: javascript google-maps mootools

我是mootools和Google Maps API V3的新手,我遇到了以下问题:

我的目标是创建一个基类来表示一个通用按钮和具有不同“点击”事件处理程序的子类,用于添加标记,多边形和折线。 因此,使用mootools,我创建了一个具有基本外观属性的基类和三个具有不同“click”事件处理程序的子类。 按钮显示正常但是当我点击一个按钮时它表示该属性未定义。这是范围问题吗?如何改进代码以调用正确的方法?


var GButton = new Class({    
    initialize: function(divObj, mapObj){
        this.div = divObj;
        this.map = mapObj;
        this.controlUI = document.createElement('DIV');
        this.controlUI.style.backgroundColor = 'white';
        this.controlUI.style.borderStyle = 'solid';
        this.div.appendChild(this.controlUI);
        this.controlText = document.createElement('DIV');
        this.controlText.style.fontFamily = 'Arial,sans-serif';
        this.controlUI.appendChild(this.controlText);
    }
});
//mootools 1.1
var GButtonMarker = GButton.extend({
    initialize: function(divObj, mapObj){
        this.parent(divObj, mapObj);
        this.controlUI.title = 'Click to add new marker';
        this.controlText.innerHTML = 'New Marker';
        google.maps.event.addDomListener(this.controlUI, 'click', this.updateControl);
    },
    //just to represent that it is pressed
    updateControl: function(){
        this.controlUI.style.backgroundColor = 'LightGrey'; //problem here! this.controlUI UNDEFINED! (while it is defined of course...)
        this.controlText.style.fontWeight = 'bold';
    } 
});

1 个答案:

答案 0 :(得分:0)

您需要将类实例绑定到回调:

google.maps.event.addDomListener(this.controlUI, 'click', this.updateControl.bind(this));

这是一个mootools包装,顺便说一句,而不是google one(如果有的话)。