繁体   English   中英

如何在SAP UI5 JS视图中从构造函数对象接收属性值

[英]How can I receive a value of property from constructor object in sap ui5 js view

我在util.Common.js文件中编码了一个对象构造函数,使我可以从任何地方接收此对象。 这里我的对象构造函数取自javascript google maps示例

jQuery.sap.declare("util.Common");

util.Common = {
    /** @constructor */
    MercatorProjection : function() {
        this.TILE_SIZE = 256;
        this.pixelOrigin_ = new google.maps.Point(this.TILE_SIZE / 2,
          this.TILE_SIZE / 2);
        this.pixelsPerLonDegree_ = this.TILE_SIZE / 360;
        this.pixelsPerLonRadian_ = this.TILE_SIZE / (2 * Math.PI);
        Map.prototype.googlemap = null;

        this.createMap = function(mapOptions){
            Map.prototype.mapOptions = mapOptions;
            Map.prototype.googlemap = new google.maps.Map(Map.prototype.map_canvas_div.getDomRef(),  
            Map.prototype.mapOptions);
        };

        this.createInfoWindowContent = function(map) {
            var numTiles = 1 << map.getZoom();
            var projection = new MercatorProjection();
            var worldCoordinate = projection.fromLatLngToPoint(chicago);
            var pixelCoordinate = new google.maps.Point(
              worldCoordinate.x * numTiles,
              worldCoordinate.y * numTiles);
            var tileCoordinate = new google.maps.Point(
              Math.floor(pixelCoordinate.x / TILE_SIZE),
              Math.floor(pixelCoordinate.y / TILE_SIZE));

            return [
            'Chicago, IL',
            'LatLng: ' + chicago.lat() + ' , ' + chicago.lng(),
            'World Coordinate: ' + worldCoordinate.x + ' , ' +
              worldCoordinate.y,
            'Pixel Coordinate: ' + Math.floor(pixelCoordinate.x) + ' , ' +
              Math.floor(pixelCoordinate.y),
            'Tile Coordinate: ' + tileCoordinate.x + ' , ' +
              tileCoordinate.y + ' at Zoom Level: ' + map.getZoom()
            ].join('<br>');
        };
        ...
    },
    ...
};

并且我试图像这样从home.js到达对象的属性之一:

var me = new util.Common.MercatorProjection();
var mapOptions = {
   zoom: 3,
   center: chicago
};
me.createMap(mapOptions);

var coordInfoWindow = new google.maps.InfoWindow();
coordInfoWindow.setContent(me.createInfoWindowContent(me.googlemap));
coordInfoWindow.setPosition(me.chicago);
coordInfoWindow.open(me.googlemap);
var listener1 = new google.maps.event.addListener(me.googlemap, 'zoom_changed', function() {
coordInfoWindow.setContent(icm.Google.Charts.values.chart.createInfoWindowContent(me.googlemap));
coordInfoWindow.open(me.googlemap);

但是当我写

me.googlemap

它说googlemap是未定义的,它不能创建信息窗口,但是我在createMap函数中创建了它,所以它不应该是未定义的,我认为我刚刚错误地达到了该值,所以我不知道如何达到该值。 任何建议将是非常好的,谢谢。

如果您查看代码,则将googlemap定义为Map的属性:

Map.prototype.googlemap = null;

但是,您的对象名称是MercatorProjection

MercatorProjection : function() {
...

我假设您希望googlemapsMercatorProjection的属性(我什至不知道Map是什么?)-如果您相应地调整代码(将Map的引用更改为MercatorProjection的引用),则对me.googlemap的引用将继续工作。

与您的问题无关,但是如果您使用的是Google Maps和UI5,请查看openui5-googlemaps

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM