Phonegap 插件 - module.exports
Phonegap plugin - module.exports
对象如何在插件的 javascript 和视图的 javascript 之间传递?我正在玩 "apache cordova 3 programming" 书中的示例代码,但我被卡住了...
在我的 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;
但是还是不行...
似乎按照定义它只分配一个对象!
"The clobbers element specifies the JavaScript object assigned to the loaded JavaScript object."
我注意到在我构建的应用程序中,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"
]
}, etc
这是一个迟到的评论,但可能对其他人有益。对我有用的是类似于以下内容:
尝试重写 plugin.js
函数如下:
module.exports.calculateMOL = function() { return 42; };
module.exports.calculateMOLLL = function() { return 42222; };
在末尾删除两个导出语句(即 module.export = mol;
和 = molll;
)
这应该允许访问上面 index.html
文件中显示的两种方法。
对象如何在插件的 javascript 和视图的 javascript 之间传递?我正在玩 "apache cordova 3 programming" 书中的示例代码,但我被卡住了...
在我的 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;
但是还是不行...
似乎按照定义它只分配一个对象!
"The clobbers element specifies the JavaScript object assigned to the loaded JavaScript object."
我注意到在我构建的应用程序中,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"
]
}, etc
这是一个迟到的评论,但可能对其他人有益。对我有用的是类似于以下内容:
尝试重写
plugin.js
函数如下:module.exports.calculateMOL = function() { return 42; };
module.exports.calculateMOLLL = function() { return 42222; };
在末尾删除两个导出语句(即
module.export = mol;
和= molll;
)
这应该允许访问上面 index.html
文件中显示的两种方法。