Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova JavaScript Plugins

I am beginning to understand the way Cordova works internally more and more; one thing I continue to struggle with is the format of the JavaScript plugins however.

I am used to writing my JavaScript as follows (which is the standard convention, as far as I am aware):

(function () {
    var version = "EXAMPLE",
        v1,
        v2,
        v3
        res;

    function somePrivateFunction(successCallback, errorCallback) {
        someOtherPrivateFunction(sc, ec);
    }

    function someOtherPrivateFunction(successCallback, errorCallback) {
        cordova.exec(sc, ec, 'SomeService', 'SomeMethod', [args]);
    }

    res = {
        VERSION: version,
        doSomething: function (sc, ec) {
            somePrivateFunction(sc, ec);    
        }
    }

    window.myPlugin = res;
}());

However, Cordova uses a format I am completely unfamiliar with. I think (and I have only heard of the term here and there) it uses something called require (judging by the declarations at the top of most plugins).

The format I often see in the official Cordova plugins are like follows:

    var argscheck = require('cordova/argscheck'),
    utils = require('cordova/utils'),
    exec = require('cordova/exec');

var myPlugin = function () {

}

myPlugin.doSomething = function(successCallback, errorCallback) {
    exec(successCallback, errorCallback, 'SomeService', 'SomeMethod', [args]);
}

myPlugin.doSomethingElse = function(successCallback, errorCallback) {
    exec(successCallback, errorCallback, 'SomeService', 'SomeOtherMethod', [args]);
}

modules.export = myPlugin;

Maybe it is because I do not have any knowledge on this require library - but I do not get it? This seems completely foreign to me, in terms of JavaScript.

What is modules, what is the cordova/[...] syntax and what does it indicate. Where are these other cordova modules defined (is that the correct terminology) and where does modules come from?

And finally, what does modules.export do? I am trying to understand the <js-module> tag of plugin.xml and the <clobbers> tag, but this is holding me back I think.

I understand that when Cordova builds the project, it inserts cordova.define surrounding the plugin.

Maybe at least someone can clarify? Thanks!

like image 863
keldar Avatar asked Feb 04 '14 17:02

keldar


People also ask

Does Cordova use JavaScript?

Cordova is an open source framework that lets you convert HTML, JavaScript, and Cascading Style Sheets (CSS) into a native application that can run on iOS, Android, and other mobile platforms.

What are different plugins in Cordova?

Plugin APIscamera: This plugin is used to provide an interface to enable the device camera. It can also select the images from the device storage. contacts: It allows access to the device contacts. device: It provides an interface to retrieve the information about the hardware and software.

Is Cordova outdated?

Cordova Android 10.1. 1 was released in September 2021. At the same time, a gap between the latest updates of Cordova and versions of operating systems is still notable — Android 10 is supported by Cordova, but Android 12 has already been released.


1 Answers

the require and exec functions are methods of the cordova object. When you install a plugin it gets wrapped in function that give access to the cordova object. Those calls are actually cordova.require and cordova.exec

Here is an example of a plugin js file before and after install:

BEFORE:

var exec = require("cordova/exec");

var VideoPlayer = {
    play: function(url) {
        exec(null, null, "VideoPlayer", "playVideo", [url]);
    }
};

module.exports = VideoPlayer;

AFTER:

cordova.define("com.dawsonloudon.videoplayer.VideoPlayer", function(require, exports, module) {

    var exec = require("cordova/exec");

    var VideoPlayer = {
        play: function(url) {
            exec(null, null, "VideoPlayer", "playVideo", [url]);
        }
    };

    module.exports = VideoPlayer;

});

Additionally, to answer about the config setup, the clobbers command secures the name space of your plugin object. From my plugin:

<js-module src="www/VideoPlayer.js" name="VideoPlayer">
    <clobbers target="VideoPlayer" />
</js-module>

This is stating the name of my JS file, and the object namespace used to call to my plugin in JS.

like image 166
Dawson Loudon Avatar answered Sep 19 '22 09:09

Dawson Loudon