Search code examples
javascripttypescriptcordova-pluginsdevextreme

Typescript to Javascript for cordova-plugins


I'm trying to call a cordova plugin from my devExtreme project. One of the methods provided by the plugin is unfortunately in TypeScript and I've tried many different angles of replicating the call in JavaScript, your help will be much appreciated in translating the following code: Plugin-Code:

function findNetworkPrinters(success: (printers: Printer[]) => void, failure: (reason: string) => void): void

What I've tried:

cordova.plugins.brotherPrinter.findNetworkPrinters(function (Printer) {
     alert(printer);
}, onSuccess, onFail);

And:

cordova.plugins.brotherPrinter.findNetworkPrinters(function (Printer) {
     alert(printer);
}, function()error{
     alert(error);
});

A brief explanation about the plugin usage:

findNetworkPrinters

Upon success, findNetworkPrinters will provide a list of printers that were discovered on the network (likely using WiFi). It is not considered an error for no printers to be found, and in this case the list will just be empty.


Solution

  • If the plugin is not already compiled, you have to compile it to JavaScript. However usually, if you get the plugin via npm, that step should have been already done.

    Considering the function signature your second try was the more correct one, but be careful about about your syntax error in the error function and that Printer and printer are to different variables. Beside that, the function actually provides not a single printer, but an array of printers. A more correct version would be:

    cordova.plugins.brotherPrinter.findNetworkPrinters(function(printers){
      printers.forEach(function(printer){
        alert(printer);
      });
    }, function(error){
      alert(error);
    });