javascript - Phonegap plugin - module.exports -
how objects passed between plugin's javascript , javascript of view? i'm playing around example code "apache cordova 3 programming" book , i'm stuck...
in plugin.xml set namespace "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 cordova application uses custom meaning of life plugin. </p> <button onclick="domol();">button1</button> <button onclick="domoll();">button2</button> </body> </html>
but when run second button works ... can give me explanation this?
i tried exporting both objects @ once like:
module.exports = molll, mol;
but still won't work...
this late comment might benefit else. worked me similar following:
try rewriting
plugin.js
functions follows:module.exports.calculatemol = function() { return 42; };
module.exports.calculatemolll = function() { return 42222; };
drop 2 export statements @ end (i.e.
module.export = mol;
,= molll;
)
this should allow 2 methods accessed shown in index.html
file above.
Comments
Post a Comment