4

When I put this code in a script.js file and include it runs fine,

but when I implement this code in a javascript file loaded by requirejs the createMapOnOverlay function is not found which is called from outside like this:

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

alarmoverlay.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 () {


};

I have to put the above code in this below script.js file which is loaded by requirejs: but the below code does not work

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() {

        };
    };
});

How do I have to derive from the google OverlayView that I can call the createMapOnOverlay function from outside which should call the setMap from the base class?

1 Answer 1

6

in 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;

}

and in main js file:

require(['AlarmOverlay'], function(AlarmOverlay) {
var overlay = new AlarmOverlay(...);
overlay.createMapOnOverlay(..);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.