首页 > 解决方案 > 如何在不同的函数中访问字符串

问题描述

我想在下面的代码中访问函数中的entityString值。具有以下属性,我想从中访问函数中的状态值。updateMarkerIconentityStringupdateMarkerIcon

id:3 号楼
类型:MetroStation
地址:Kashmiri_GateMetro纬度:
28.6674
经度:77.2282
房间数:6
状态:危急
状态:温度高 --

function() {

  "use strict";

  var icon;

  MashupPlatform.wiring.registerCallback("entityInput", function(entityString) {
    var entity = JSON.parse(entityString);
    var coordinates = null;
    var coord_parts = null;
    var coordinates_pref = MashupPlatform.prefs.get('coordinates_attr');
    var attributes = coordinates_pref.split(new RegExp(',\\s*'));
    if (attributes.length < 1) {
      return;
    } else if (attributes.length >= 2 && entity[attributes[0]] != null && entity[attributes[1]] != null) {
      coord_parts = [
        entity[attributes[0]],
        entity[attributes[1]]
      ];
    } else if (entity[attributes[0]]) {
      coord_parts = entity[attributes[0]].split(new RegExp(',\\s*'));
    }

    if (coord_parts != null && coord_parts.length === 2) {
      coordinates = {
        system: "WGS84",
        lat: parseFloat(coord_parts[0]),
        lng: parseFloat(coord_parts[1])
      };
    }

    if (coordinates) {
      MashupPlatform.wiring.pushEvent("poiOutput", JSON.stringify(entity2poi(entity, coordinates)));
    }

  });

  var entity2poi = function entity2poi(entity, coordinates) {
    var poi = {
      id: entity.id,
      icon: icon,
      tooltip: entity.id,
      data: entity,
      infoWindow: buildInfoWindow.call(this, entity),
      currentLocation: coordinates
    };

    return poi;
  };

  var internalUrl = function internalUrl(data) {
    var url = document.createElement("a");
    url.setAttribute('href', data);
    return url.href;
  };
  var buildInfoWindow = function buildInfoWindow(entity) {
    var infoWindow = "<div>";
    for (var attr in entity) {
      infoWindow += '<span style="font-size:12px;"><b>' + attr + ": </b> " + entity[attr] + "</span><br />";
    }
    infoWindow += "</div>";

    return infoWindow;
  };
  var updateMarkerIcon = function updateMarkerIcon() {
    icon = MashupPlatform.prefs.get('marker-icon');
    if (icon == '') {
      icon = internalUrl('images/icon.png');
    }
  };
  MashupPlatform.prefs.registerCallback(updateMarkerIcon);
  // Init initial marker icon
  updateMarkerIcon();

}

标签: javascript

解决方案


您必须更改您的updateMarkerIcon()函数以接受一个参数e.g. var updateMarkerIcon = function updateMarkerIcon(<someParameterHere>),然后在您的.registerCallback()回调函数中调用该函数,并传递entityString给它e.g. updateMarkerIcon(entityString)


推荐阅读