Cordova JavaScript 插件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21559102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cordova JavaScript Plugins
提问by keldar
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.
我开始越来越了解 Cordova 内部的工作方式;然而,我一直在努力解决的一件事是 JavaScript 插件的格式。
I am used to writing my JavaScript as follows (which is the standard convention, as far as I am aware):
我习惯于按如下方式编写 JavaScript(据我所知,这是标准约定):
(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).
但是,Cordova 使用了一种我完全不熟悉的格式。我认为(而且我只在这里和那里听说过这个术语)它使用了一种叫做require
(从大多数插件顶部的声明来判断)的东西。
The format I often see in the official Cordova plugins are like follows:
我经常在 Cordova 官方插件中看到的格式如下:
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.
也许是因为我对这个require
库一无所知- 但我不明白?就 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?
什么是模块,什么是cordova/[...]
语法以及它表示什么。这些其他cordova模块在哪里定义(这是正确的术语)以及modules
来自哪里?
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.
最后,有什么作用modules.export
?我试图理解<js-module>
标签plugin.xml
和<clobbers>
标签,但我认为这阻碍了我。
I understand that when Cordova builds the project, it inserts cordova.define
surrounding the plugin.
我知道当 Cordova 构建项目时,它会cordova.define
围绕插件插入。
Maybe at least someone can clarify? Thanks!
也许至少有人可以澄清?谢谢!
回答by Dawson Loudon
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
require 和 exec 函数是cordova 对象的方法。当您安装插件时,它会被封装在可以访问cordova 对象的函数中。这些调用实际上是 cordova.require 和 cordova.exec
Here is an example of a plugin js file before and after install:
这是安装前后插件js文件的示例:
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:
此外,要回答有关配置设置的问题,clobbers 命令会保护您的插件对象的名称空间。从我的插件:
<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.
这说明了我的 JS 文件的名称,以及用于在 JS 中调用我的插件的对象命名空间。