How to replace deprecated jQuery functions?

Viewed 511

I am trying to display a map in my UI5 app using a formatter.js where I need to put the address with the map URL together.
In the old world the code should look like the following:

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);
},

How should I replace the deprecated function? Where should I add the new code?

1 Answers

If you look at the API documentation for jQuery.sap.encodeURL, you see it states to use the module sap/base/security/encodeURL now.

Usage example from documentation:

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
});

Usage in the 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;
    }
  };
});
Related