2013-05-16 78 views
4

當我把這個代碼放在一個文件的script.js,包括它運行正常加載,原型功能通過requirejs

,但是當我通過requirejs加載javascript文件執行這段代碼createMapOnOverlay功能沒有發現這就是所謂從外部是這樣的:

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


}; 

我必須把上面的代碼在這下面是由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?

回答

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(..); 
} 
+0

是是是。返回失蹤:) – Elisabeth