如何在插件的javascript和视图的javascript之间传递对象?我在玩"apache 3编程“书中的示例代码,我被困住了.
在我的plugin.xml中,我将名称空间设置为"mool“
<js-module src="plugin.js" name="moool">
<clobbers target="mool" />
</js-module>plugin.js
var mol = {
calculateMOL : function() {
return 42;
}
};
var molll = {
calculateMOLLL : function() {
return 42222;
}
};
module.exports = molll;
module.exports = mol;index.html
<!DOCTYPE html> <html>
<head>
<title>Meaning of Life Demo</title>
<meta http-equiv="Content-type" content="text/html;
charset=utf-8">
<meta name="viewport" content="user-scalable=no,
initial-scale=1, maximum-scale=1, minimum-scale=1,
width=device-width;" />
<script type="text/javascript" charset="utf-8"
src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
function onBodyLoad() {
document.addEventListener("deviceready", onDeviceReady,
false);
};
function onDeviceReady() {
//alert("onDeviceReady");
};
function doMOL() {
var res = mool.calculateMOL();
alert('Meaning of Life = ' + res);
};
function doMOLL() {
var res = mool.calculateMOLLL();
alert('Meaning of Life = ' + res);
};
</script>
</head>
<body onload="onBodyLoad()">
<h1>MoL Demo</h1>
<p>This is a Cordova application that uses my custom
Meaning of Life plugin. </p>
<button onclick="doMOL();">Button1</button>
<button onclick="doMOLL();">Button2</button>
</body>
</html>但当我运行第二个按钮时.有人能给我解释一下吗?
我已经尝试同时导出两个对象,例如:
module.exports = molll, mol;但还是没用..。
发布于 2015-07-14 12:19:40
似乎按照定义,它只分配一个对象!
“clobbers元素指定分配给加载的JavaScript对象的JavaScript对象。”
发布于 2016-01-17 03:02:41
这是一个迟来的评论,但可能会使其他人受益。对我起作用的是类似于以下几点:
plugin.js函数:
module.exports.calculateMOL = function() { return 42; };
module.exports.calculateMOLLL = function() { return 42222; };module.export = mol;和= molll;)这应该允许访问这两个方法,如上面的index.html文件所示。
发布于 2015-09-18 01:00:52
我注意到,在我构建的应用程序中,module.exports属性正在接受一个数组,如下所示。这样你就可以把你的物品都放进去了(?)(我只是在这个片段中显示数组的一个对象。)
cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
{
"file": "plugins/org.apache.cordova.dialogs/www/notification.js",
"id": "org.apache.cordova.dialogs.notification",
"merges": [
"navigator.notification"
]
}, etchttps://stackoverflow.com/questions/31405801
复制相似问题