prototype functions in a javascript file loaded by requirejs - Stack Overflow

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

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?

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?

Share Improve this question asked May 16, 2013 at 14:27 ElisabethElisabeth 21.3k57 gold badges211 silver badges325 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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(..);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745199793a4616268.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信