The more I delve into the inner workings of Cordova, the clearer it becomes to me. Yet, one area that continues to perplex me is the structure of JavaScript plugins.
Typically, I write my JavaScript code as follows, adhering to what I believe is the standard convention:
(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, I have noticed that Cordova employs a format with which I am completely unfamiliar. It seems to utilize something referred to as require
, based on the declarations in most plugins.
The format commonly used in official Cordova plugins appears like this:
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;
This approach involving require
library is confusing to me as I lack knowledge in this area. It feels entirely foreign in terms of JavaScript.
I'm puzzled by concepts such as modules, the cordova/[...]
syntax, and their significance. Where are these other cordova modules specified and what is the role of modules
?
Moreover, can someone explain the purpose of modules.export
? I find it challenging to grasp the <js-module>
tag in the plugin.xml
and the <clobbers>
tag while struggling with this aspect.
It's my understanding that Cordova includes cordova.define
around the plugin during project compilation.
If anyone could shed some light on this, I'd greatly appreciate it!