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 pletely 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 pletely 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
e 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!
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 pletely 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 pletely 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
e 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!
Share Improve this question edited Feb 4, 2014 at 17:52 Dawson Loudon 6,0292 gold badges29 silver badges31 bronze badges asked Feb 4, 2014 at 17:18 keldarkeldar 6,25211 gold badges54 silver badges84 bronze badges 4- 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 – Dawson Loudon Commented Feb 4, 2014 at 17:21
- Louis, this doesn't have to do with RequireJS, per my explanation. I have removed the tag – Dawson Loudon Commented Feb 4, 2014 at 17:51
-
With the plugin framework, unless you have a specific need, you only need to be concerned with the
exec
mand which is the bridge function between js and native code. – Dawson Loudon Commented Feb 4, 2014 at 18:21 -
@keldar, no one answered the part of this question about
modules.export
did you ever figure that out? – E.A.T Commented Jul 16, 2015 at 23:41
1 Answer
Reset to default 13the 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(".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 mand 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.