4

当我将此代码放入 script.js 文件并包含它时运行良好,

但是当我在 requirejs 加载的 javascript 文件中实现此代码时,找不到从外部调用的 createMapOnOverlay 函数,如下所示:

var overlay = new AlarmOverlay(...);
overlay.createMapOnOverlay(..);

警报覆盖.js:

AlarmOverlay.prototype = new google.maps.OverlayView();

/* constructor */
function AlarmOverlay(bounds, alarmNumber, alarmCssClass) {

    // initialize all properties for an alarm
    this.bounds = bounds;
    this.alarmNumber = alarmNumber;
    this.alarmCssClass = alarmCssClass;
}

AlarmOverlay.prototype.createMapOnOverlay = function(map) {
    // Explicitly call setMap on this overlay
    this.map = map;
    this.setMap(map);
};

AlarmOverlay.prototype.onAdd = function () {


};

AlarmOverlay.prototype.draw = function () {


};

我必须将上面的代码放在由requirejs加载的下面的script.js文件中:但下面的代码不起作用

define(function() {
    return function AlarmOverlay(bounds, alarmNumber, alarmCssClass) {

        var self = this;

        self.prototype = new google.maps.OverlayView();

        self.bounds = bounds;
        self.alarmNumber = alarmNumber;
        self.alarmCssClass = alarmCssClass;      


        //AlarmOverlay.prototype.createMapOnOverlay = function(map) {      
           self.map = map;
           self.setMap(map);

        //};

        AlarmOverlay.prototype.onAdd = function() {

        };

        AlarmOverlay.prototype.draw = function() {

        };
    };
});

我如何必须从谷歌 OverlayView 派生,我可以从外部调用 createMapOnOverlay 函数,该函数应该从基类调用 setMap?

4

1 回答 1

6

在 AlarmOverlay.js 中:

define(['google'], function(google) {

AlarmOverlay.prototype = new google.maps.OverlayView();

/* constructor */
function AlarmOverlay(bounds, alarmNumber, alarmCssClass) {

    // initialize all properties for an alarm
    this.bounds = bounds;
    this.alarmNumber = alarmNumber;
    this.alarmCssClass = alarmCssClass;
}

AlarmOverlay.prototype.createMapOnOverlay = function(map) {
    // Explicitly call setMap on this overlay
    this.map = map;
    this.setMap(map);
};

AlarmOverlay.prototype.onAdd = function () {


};

AlarmOverlay.prototype.draw = function () {


};


return AlarmOverlay;

}

在主 js 文件中:

require(['AlarmOverlay'], function(AlarmOverlay) {
var overlay = new AlarmOverlay(...);
overlay.createMapOnOverlay(..);
}
于 2013-05-16T20:00:32.837 回答