Dennis Nedry
Dennis Nedry

Reputation: 4748

Phonegap plugin - module.exports

How are objects passed between the plugin's javascript and the javascript of the view? I'm playing around with an example code from the "apache cordova 3 programming" book and i'm stuck...

In my plugin.xml I set the namespace to "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>

But when I run it only the second button works ... can somebody give me an explanation to this?

I already tried exporting both objects at once like:

module.exports = molll, mol;

but it still won't work...

Upvotes: 0

Views: 3303

Answers (3)

Ash
Ash

Reputation: 208

This is a late comment but might benefit someone else. What worked for me was something similar to the following:

  1. Try rewriting the plugin.js functions as follows:

    module.exports.calculateMOL = function() { return 42; };

    module.exports.calculateMOLLL = function() { return 42222; };

  2. Drop the two export statements at the end (i.e. module.export = mol; and = molll;)

This should allow the two methods to be accessed as shown in the index.html file above.

Upvotes: 1

pdschuller
pdschuller

Reputation: 584

I notice that in an app I had built, the module.exports property is taking an array, like below. That would allow you to put both your items in there(?) (I am just showing one object of the array in this snippet.)

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

Upvotes: 0

Dennis Nedry
Dennis Nedry

Reputation: 4748

It seems as if per definition it only assignes one object!

"The clobbers element specifies the JavaScript object assigned to the loaded JavaScript object."

Upvotes: 0

Related Questions