如何替换不推荐使用的jQuery函数?

问题描述

我正在尝试使用formatter.js在UI5应用程序中显示地图,我需要将地址和地图URL放在一起。
在旧世界中,代码应如下所示:

formatMapUrl: function(sstreet,sZIP,sCity,sCountry) {
  return "https://maps.googleapis.com/maps/api/staticmap?zoom=13&size=640x640&markers="
    + jQuery.sap.encodeURL(sstreet + "," + sZIP +  " " + sCity + "," + sCountry);
},

如何替换不推荐使用的功能?我应该在哪里添加代码

解决方法

如果您查看jQuery.sap.encodeURL的API文档,就会发现它指出现在使用模块sap/base/security/encodeURL

文档中的用法示例:

sap.ui.require(["sap/base/security/encodeURL"],function(encodeURL) {
   var sEncoded = encodeURL("a/b?c=d&e");
   console.log(sEncoded); // a%2fb%3fc%3dd%26e
});

在formatter.js中的用法:

sap.ui.define([
  "sap/base/security/encodeURL"
],function (encodeURL) {
  "use strict";

  return {
    formatMapUrl: function(sStreet,sZIP,sCity,sCountry) {
      var sBaseUrl = "https://maps.googleapis.com/maps/api/staticmap?zoom=13&size=640x640&markers=";
      var sEncodedString = encodeURL(sStreet + "," + sZIP +  " " + sCity + "," + sCountry);
      return sBaseUrl + sEncodedString;
    }
  };
});